This is a result of short-circuit evaluation.
The expression ++x
evaluates to 2
, and the compiler knows that 2 || anything
always evaluates to 1
("true") no matter what anything
is. Therefore it does not proceed to evaluate anything
and the values of y
and z
do not change.
If you try with
x=-1;
y=z=1;
You will see that y
and z
will be incremented, because the compiler has to evaluate the right hand side of the OR to determine the result of the expression.
Edit: asaerl answered your follow-up question in the comments first so I 'll just expand on his correct answer a little.
Operator precedence determines how the parts that make up an expression bind together. Because AND has higher precedence than OR, the compiler knows that you wrote
++x || (++y && ++z)
instead of
(++x || ++y) && ++z
This leaves it tasked to do an OR between ++x
and ++y && ++z
. At this point it would normally be free to select if it would "prefer" to evaluate one or the other expression first -- as per the standard -- and you would not normally be able to depend on the specific order. This order has nothing to do with operator precedence.
However, specifically for ||
and &&
the standard demands that evaluation will always proceed from left to right so that short-circuiting can work and developers can depend on the rhs expression not being evaluated if the result of evaluating the lhs tells.