+ 9
Why value of y and z is not incremented? i.e. output should be 1222
int x=1,y=1,z=1; cout <<(++x||++y&&++z); cout <<x <<y <<z; output is:- 1211
5 Answers
+ 12
Code ninja is correct.
From: http://www.cplusplus.com/doc/tutorial/operators/
When using the logical operators, C++ only evaluates what is necessary from left to right to come up with the combined relational result, ignoring the rest. Therefore, in the last example ((5==5)||(3>6)), C++ evaluates first whether 5==5 is true, and if so, it never checks whether 3>6 is true or not. This is known as short-circuit evaluation, and works like this for these operators
&&: if the left-hand side expression is false, the combined result is false (the right-hand side expression is never evaluated).
||: if the left-hand side expression is true, the combined result is true (the right-hand side expression is never evaluated).
+ 6
If i am not wrong then, as || is true if either of the cond. is true hence as during evaluation from left to right as compiler sees that ++x is true hence it does not sees the rest part and evaluates answer to true.
correct me if i am wrong.
thanks
+ 6
yes logical OR || follows short-circuit evaluation...so it doesn't get into the other expression.
https://www.sololearn.com/discuss/978299/?ref=app
+ 6
|| and && are lazy evaluation operators. If you know the result, they'll skip the next operation. If you don't want lazy evaluation use | or &
+ 3
@Pegasus | and & are bitwise operators, they perform the logical operations on each bit.