0

Why does this operation equal true even though it should have no output because its false because true + false = false?

int age = 42; int money = 4; if (age > 21 && money > 500) { cout << "Welcome" << endl; }

2nd Dec 2016, 4:57 AM
Talkiestcobra6
Talkiestcobra6 - avatar
3 odpowiedzi
+ 2
I tried this code and I have no output, maybe it is a bug for you. If you want, you can try this on the 'if' statement instead : if ( (age > 21) && (money > 500) ) It is no different from what you wrote before, but maybe can help to solve the problem for you because sometimes programming tools are a little bit faulty.
2nd Dec 2016, 5:02 AM
Wen Qin
Wen Qin - avatar
+ 1
In this if condition, you're using logical AND operator, which means both conditions should be true then only IF block will execute. If anyone of the condition false then IF block won't execute. Please note that AND is not equal to addition. Change && to || (logical OR) then it will behave like you expected. Basically OR operator is behaves like addition. Hope you understand well.
2nd Dec 2016, 5:04 AM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
0
true dat and thanks
2nd Dec 2016, 5:03 AM
Talkiestcobra6
Talkiestcobra6 - avatar