One comparison answers one question. Real rules usually have several parts — under 12 or 65 and over, between 10 and 20, not a weekend — and boolean logic is how you join the parts into a single condition.
And, or, not
| Condition | Also written | True when |
|---|---|---|
a && b | a and b | both a and b are true |
a || b | a or b | at least one of them is true |
!a | not a | a is false |
The words are part of C++ too: age < 12 or age >= 65 compiles and means exactly
the same as age < 12 || age >= 65. Use whichever you like — but expect the
symbols in almost every solution you read, so know both.
C++ reads && and || left to right and stops as soon as the answer is
settled: if the left side of an && is false, nothing can rescue it, so the
right side is never looked at.
The easiest way to get them right is to say the rule out loud and listen for the
word. Free entry for visitors under 12 or 65 and over is
age < 12 || age >= 65. Try && there and nobody gets in free — no one is under
12 and over 65 at the same time.
Now wire it up. Each switch below is a bool, and every wire carries true or
false — a glowing wire is true. Flip the switches, light the lamp, and watch
for the part C++ never looks at.
