Skip to main content
๐Ÿ“œ 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 it
  • a && b โ€” and: true only when both are true
  • a || 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 Code
Evaluate everythingRelational guards, short-circuit safety, truth tables.
4 challenges ยท ยท ~40 min