Having trouble understanding && and ||

closed account (z09TURfi)
I have been reading cplusplus beginner tutorials and have been ok understanding basics. Well i'm stuck on this example and since I have no laptop at the moment due to charger issues I just been writing all this in paper since I can not debug.(I am on my phone by the way) Well anyways here is the problem. ( (5 == 5) && (3 > 6 ) // evaluates to false. First of all I understand the 5 == 5 part because yes five does equal to five so thats true and I also understand 3 > 6 is false because three isn't greater than six at all. I just don't understand how it evaluates to just false together? Same thing with
( (5 == 5) || (3 > 6) ) // evaluates to true. Instead of ampersands, it's using the pipe signs aswel. Maybe if I could debug this i'll understand...
&& means 'and', so if a and b are both true, it's true, otherwise it's false.
|| means 'or', so if a or b is true, the whole thing is true.
Last edited on
&& very literally means 'and'. The whole expression will evaluate to true, if both condition1 && condition2 are true. If either is false, that means the whole statement is false. If I say "1 is greater then 0 AND 1 is greater then 2", you would (hopefully) say that is false, because the second part is false.

|| very literally means 'or'. condition1 || condition2 will evaluate to true if either condition1 OR condition2 are true. It can be either one, the other, or both, as long as 1 is true, the whole thing is true.
closed account (z09TURfi)
Wow that was easy, thank you for breaking it down.
Topic archived. No new replies allowed.