0
What is the output of this code? int x = 1, y = 1, z = 1; cout << (++x || ++y && ++z); cout << x << y << z;
I got this question in a quiz and I can't figure out in what order does the conditions are checked. Ans is 1211 why only X's value is changed?
4 Réponses
+ 2
Vedansh Mittal yes, answer should be 1211...
why :
there are two cout statements , out of which first one is to show boolean values... it might have answer 1 or 0 for first cout..
also note that if || (or operator) is there and first condition is passed, other part never gets evaluated... so, ++x is only evaluated and as ++x is not zero, cout prints 1.
++x made x as 2.
as only ++x is evaluated, y and z is still 1 and 1.
so, output is 1211..
+ 1
Is the asnwer 1211 or 211? Of it is the former, I don't know. But if it is the latter, then it is because the compiler saw two options. It can increase x or it can increase both y and z.
Since there were no conditions to be satisfied, for it to choose one or the other, it chose to increase the first.
If the code was ++x && ++y && ++z, all would be increased.
0
ok..... well the answer is 1211 because there are two cout statements and the initial '1' is printed because if the first one
0
yeah... thanks to both of you