๐ WAYPOINT LESSON
Logical operators and short-circuit
โญ beginnerโณ 14 min read๐ Lesson 10 of 256
!, &&, ||, truth tables, and the short-circuit guard pattern.
Logical operators combine booleans:
!aโ not: flips ita && bโ and: true only when both are truea || bโ or: true when at least one is true
Precedence: ! before && before ||.
boolean hasTicket = true, isStudent = false;
System.out.println(hasTicket && !isStudent); // true
System.out.println(!hasTicket || isStudent); // false
Short-circuit evaluation โ && and || stop as soon as the answer is
known, and the right side may never run:
int d = 0;
if (d != 0 && 10 / d > 2) { ... } // safe: right side skipped when d == 0
This is not just an optimization โ it is the standard guard pattern:
check the risky condition after && and the cheap range-check before.
|| short-circuits too: if the left side is true, the right side never
runs.
Truth-table tracing is an exam staple: for each combination, write the value of each sub-expression, then combine.
โก Now practice
Ready to CodeEvaluate everythingRelational guards, short-circuit safety, truth tables.
4 challenges ยท ยท ~40 min