← Back to Week 1 Hub
Logical Operators: && || !
Workbook 1c, p.100 — Building compound conditions with AND, OR, and NOT
Operator Reference
&&
AND
Both sides must be true for the result to be true
🚪🚪
Two doors in a hallway — both must be open to walk through.
||
OR
At least one side must be true for the result to be true
🚪 🚪
Two separate exits — either one gets you out.
!
NOT
Flips true to false and false to true
💡
A light switch — flip it and the opposite happens.
Interactive Truth Tables — Toggle inputs and watch the result
&& (AND)
Both must be true
false && false
Result: false
|| (OR)
At least one must be true
false || false
Result: false
! (NOT)
Flips the value
!false
Result: true
Real-World Scenario — Ticket Pricing
Common Mistakes
Watch out for these:
- Single vs Double: Use
&& not & and || not |. The single versions are bitwise operators — they work differently and don't short-circuit.
- Operator Precedence:
&& is evaluated before ||. So a || b && c means a || (b && c), not (a || b) && c. When in doubt, use parentheses to make your intent clear.
- Double Negatives:
!(!isActive) is just isActive. Keep conditions simple and readable.
Toggle the switches and move the slider to see how logical operators evaluate in real time.