Reputation:
Consider the code:
int i=2, j=3;
if(i<5 or (++i==j))
cout << "i=" << i;
The output is:
i=2
Why not using parenthesis have any effect in the above condition? Why not the output is 3?
compiler: g++ 4.8.2 on Ubuntu 14.04LTS
Upvotes: 0
Views: 86
Reputation: 234635
or
is the same as ||
, which performs a short-circuit evaluation from left to right. This means that once the result of the expression containing ||
is known, evaluation stops.
(||
is also a sequencing point, so the behaviour is well-defined even in the case where i >= 5
).
Since i < 5
is true
, the other expression is not computed; so i
is not incremented.
Upvotes: 6