+ 1

Can anyone explain the output?

#include <iostream> using namespace std; int main() { int x=1,y=1,z=1; cout<<(++x||++y&&++z); cout<<x<<y<<z; return 0; }

20th Jan 2018, 4:38 PM
Pranav Dixit
Pranav Dixit - avatar
6 odpowiedzi
+ 2
I’m not sure how C++ handles integers as booleans, but the second cout will output 222 because x y and z were incremented in the line before.
20th Jan 2018, 4:42 PM
Jacob Pembleton
Jacob Pembleton - avatar
+ 1
No, the compiler doesnt have to test z because the OR-Condition will always be true if one operand, here x, is true. Whether z is true or false simply doesnt matter for the compiler, so he skips checking it. Did you read through the post I linked? I explained it in detail there...
20th Jan 2018, 4:51 PM
Shadow
Shadow - avatar
0
Here you find a similar case that should aswer your question: https://www.sololearn.com/discuss/992610/?ref=app
20th Jan 2018, 4:45 PM
Shadow
Shadow - avatar
0
i can't understand why z is not incremented
20th Jan 2018, 4:47 PM
Pranav Dixit
Pranav Dixit - avatar
0
In ++x||++z only x is increased and result is one but after that to evaluate and second condition must be checked i.e ++z
20th Jan 2018, 4:49 PM
Pranav Dixit
Pranav Dixit - avatar
0
ohk now i understand the precedence of && is greater than ||. Thanks for helping
20th Jan 2018, 4:55 PM
Pranav Dixit
Pranav Dixit - avatar