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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.