Here is my problem: x is 11, y is 6 and z is 1.
what is the value of w?
w = x == y || x != y && z > x;
I dont understand how to decide whether I should evaluate the || or the && first.
Here is my problem: x is 11, y is 6 and z is 1.
what is the value of w?
w = x == y || x != y && z > x;
I dont understand how to decide whether I should evaluate the || or the && first.
&&
has higher precedence than ||
. What else does one need to know?
x == y || x != y && z > x;
x != y
evaluates to true
, and z>x
evaluates to false
so x != y && z > x
evaluates to false
. So we have:
x == y || false;
Now since x==y
is false
we get:
false || false;
However order of evaluation does not depend on precedence if that is where you were confused. For a deeper understanding about what i said please read the answer here to my question by Jerry Coffin.
That would be evaluated like this w = (x == y) || (x != y && z > x)
wikipedia has the full order for all operations http://en.wikipedia.org/wiki/Order_of_operations#Programming_languages
Take a look at http://www.isthe.com/chongo/tech/comp/c/c-precedence.html for the order of precedence. In this case &&
has higher precedence than ||
.
In case you are not sure, just use bracket. Putting a bracket is much more useful rather than having such a headache.