my program doesnt seem following my if statement condition, i have || and && in my statement... is there a better way to do this??thanks

this just an example of my code...

else if (age>19&&bmires2>=18.50||bmires2<=25.00)
              {messagebox.show("normal weight");
               }
      else if (age<19&&bmires2<18.50||bmires2>13.25)
{                 messagebox.show("underweight");
}

just put your && statement in brackets

else if ((age>19 && bmires2>=18.50) || bmires2<=25.00)

cant i do this??

else if ((age>19)&& bmires2>=18.50||bmires2<=25.00)

?? im comparing the bmires2. its range must be from 18.50 to 25.00..
or both of them should be inside the bracket?

else if ((age>19)&&(bmires2>18.50||bmires2<=25.00))

whats the best way to compare my statement with && and ||

Edited 5 Years Ago by kapojian: n/a

Your using the OR operator when you should be using an AND operator. If you say bmires2 >= 18.50 || bmires2 <= 25.00 , it will always evaluate to true. Therefore the only thing that would affect the statement is age > 19 .

Just for instance, if bmires2 = 10.00 , then bmires2 >= 18.50 is false, but bmi <= 25.00 is true. Given this, true AND false will evaluate to true.

In my personal experience, whenever I am using a mixture of AND's and OR's, I use parentheses to avoid any confusion.

Edited 5 Years Ago by nmaillet: n/a

Comments
Good!

is there another way to have a bmires2 a range not less than 18.50 and not greater than 25???

else if ((age>19)&&(bmires2>18.50||bmires2<25))

is this right?

what do you suggest??? thanks..

Edited 5 Years Ago by kapojian: n/a

It is an issue with operators, not parentheses in this case:

else if (age > 19 && bmires2 > 18.50 && bmires2 < 25.00)

This would work for you. Since you would only be using the AND operator, there is no need to worry about the order in which the statements are evaluated. You may want to add parentheses for readability, such as:

else if (age > 19 && (bmires2 > 18.50 && bmires2 < 25.00))

but this is not necessary. This just makes things blatantly obvious for others. Take a look at the link ddanbe posted.

i guess i got confused with the logical operations in my statement. thanks for your great replies.... thanks a lot.. got clarified and solved

This question has already been answered. Start a new discussion instead.