+ 1
Why is the output of this code '1'?
cout << 1&&0||1||0;
4 Answers
+ 21
The truth table for AND (&&) logical operator for two operands.
a b output
-------------------
0 0 0
0 1 0
1 0 0
1 1 1
The truth table for OR (||) logical operator for two operands.
a b output
-------------------
0 0 0
0 1 1
1 0 1
1 1 1
// original expression
cout << 1 && 0 || 1 || 0;
// their precedence
cout << (((1 && 0) || 1) || 0);
cout << ((0 || 1) || 0);
cout << (1 || 0);
cout << 1;
+ 13
Break it down:
Anything AND with 0 will be 0
1&&0 =0
Entire expression would be 0||1||0.
Anything OR with 1 will be 1
Thats why whole expression = 1
+ 6
1 && 0 || 1 1||0
I | |
true. && true. && true. = true = 1.
+ 1
yess babak is right .