Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
What goes first? && (AND) or || (OR)?
I'm supposed to figure out what will a program read first.
And statements or Or statements, and I haven't found the answer online.
Note that I'm talking about IF statements, like for example:
if ( p > 2 && r == 2 || j < 5 && k == 10 && m <= 3 || h >=2 || v < 17 ) {
DO SOMETHING
}
else {
DO SOMETHING ELSE
}
3 Answers
- 1 decade agoFavorite Answer
Logical AND && comes first to Logical OR ||.
Associativity of both these operators is left to right meaning if you encounter && operator twice or more, then first && operator in the order of appearance from left to right will be executed first. Similarly with ||.
e.g.,
<expression1> && <expression2> && <expression3>
Here ANDING between expression1 and expression2 is performed first and it's result is taken to AND with expression3.
Similarly goes to ||.
if ( p > 2 && r == 2 || j < 5 && k == 10 && m <= 3 || h >=2 || v < 17
Here p > 2 && r == 2 is performed first, let's say it results in x
j < 5 && k == 10 is performed second, let's say it results in y
y && m <= 3 is performed third, let's say it results in z
x || z is performed fourth, let's say it results in a
a || h >= 2 is performed fifth, let's say it results in b
finally b || v < 17 will be performed.
you might be interested in knowing the precedence and priorities of execution of other operators in c and their associativity.Following is the link.
Source(s): http://www.difranco.net/cop2220/op-prec.htm - jimbotLv 61 decade ago
I think it's right to left, not sure though. I think it goes:
v < 17? do something
h >= 2? do something
j < 5 && k == 10 && m <= 3? do something
p > 2 && r == 2? do something
else do something else.
- Anonymous1 decade ago
I think it's order of appearance