Why is the output of this code '1'? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is the output of this code '1'?

cout << 1&&0||1||0;

15th Oct 2017, 12:29 PM
Pablo Bustamante
Pablo Bustamante - avatar
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;
15th Oct 2017, 1:35 PM
Babak
Babak - avatar
+ 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
15th Oct 2017, 12:32 PM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 6
1 && 0 || 1 1||0 I | | true. && true. && true. = true = 1.
15th Oct 2017, 3:31 PM
Andrew
Andrew - avatar
+ 1
yess babak is right .
15th Oct 2017, 5:39 PM
Zshn Khn
Zshn Khn - avatar