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");
}``````
5
Contributors
9
Replies
13
Views
6 Years
Discussion Span
Last Post by kapojian

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 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 by nmaillet: n/a

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 by kapojian: n/a

This is right if you use && instead of ||.
A bmi of 0 would pass your or test. Check it!

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.

Depends on what he has in mind to put together and what to seperate from other data.

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