+ 2

Is true && false == 1?

Still not cleared on this https://code.sololearn.com/cEvv139DcEns/?ref=app

8th Apr 2018, 7:51 AM
E_E Mopho
E_E Mopho - avatar
11 Answers
+ 2
if anyone noticed while using logical operator for evaluation we always use them in parentheses such as if (x<y). use this: cout<<(true&&false); ur result will be 0 as expected.
8th Apr 2018, 11:53 AM
gurpreet kapoor
gurpreet kapoor - avatar
+ 4
I think in int and bool got right output rather cout also give correct output look in cout first call true then &&false thats why output is 1 as true value You can check this as cout << "but if u directly output true&&false u get "<< false&&true ; now output is 0 as value of false..
8th Apr 2018, 7:59 AM
Scooby
Scooby - avatar
+ 3
false && false -> false false && true -> false true && false -> false true && true -> true
8th Apr 2018, 8:03 AM
Timon Paßlick
+ 3
In the case of cout, operator precedence is the issue. cout << "but if u directly output true&&false u get "<< true&&false; is equivalent to ( cout << "but if u directly output true&&false u get "<< true )&&false;
8th Apr 2018, 8:07 AM
Dennis
Dennis - avatar
+ 2
&& checks if both sides are true, but here, just one side is true. So it returns false.
8th Apr 2018, 7:59 AM
Timon Paßlick
+ 2
Coder Always these words are confusing. there are 2 ops to compare: && and "and". this is made cause different priorities in the op hirarchy. I think the prob here is the handling of the stream. To get it clear there would be brackets a plus.
8th Apr 2018, 11:17 AM
Heinrich Weber
+ 1
and operator is used for comparing two sides and if either one results false the output is false
8th Apr 2018, 8:07 AM
Durg@😊
+ 1
correct as all of you mentioned guys. problem is using expressions in COUT in general. in this cases with COUT I recommend you always to use variables, as you did in first two examples. It will be more clear to read and to find possible mistakes. b = true && false COUT << b
8th Apr 2018, 9:16 AM
Miroslav M
Miroslav M - avatar
+ 1
b is a throw-away variable and causes confusion as it's another layer of indirection. Just do std::cout << (true && false);
8th Apr 2018, 9:20 AM
Timon Paßlick
+ 1
std::cout << std::boolalpha; Now you don't get 0 or 1 but true or false.
8th Apr 2018, 12:06 PM
Timon Paßlick
0
0&&1 results 0 only as one side is false and the other is true
8th Apr 2018, 8:01 AM
Durg@😊