AP Computer Science A · Unit 2 lecture · Part 4 of 12 · 9 min

2.5 Compound Boolean expressionsAnd, or, not, and short-circuiting

Mr. Thompkins
"Free entry if you're under 12 or over 65." "You can drive if you're 16 and you have a license." Real tests combine conditions. Java has three logical operators for that, and one sneaky behavior you must know.

The logical operators combine boolean values. a && b (and) is true only when both a and b are true. a || b (or) is true when at least one is true, including when both are. !a (not) flips a value: !true is false.

aba && ba || b!a
truetruetruetruefalse
truefalsefalsetruefalse
falsetruefalsetruetrue
falsefalsefalsefalsetrue
A truth table lists every combination of inputs. && needs both. || needs at least one. ! flips.

A range test needs two comparisons joined by &&: x >= 1 && x <= 10 is true for 1 through 10. You can't write 1 <= x <= 10 in Java; it doesn't compile. Outside a range uses ||: x < 1 || x > 10.

Precedence: ! comes first, then &&, then ||. So a || b && c means a || (b && c). Relational operators like < and == come before && and ||, so x > 0 && y > 0 needs no extra parentheses. When in doubt, add parentheses. They never hurt.

?Wait, what's precedence again?
Precedence is the order in which Java applies operators when there are no parentheses. In Unit 1, * and / came before + and -. The full order for this unit: ! first (it sticks to the value right after it), then arithmetic, then relational operators like < and ==, then &&, and || last.

Short-circuit evaluation

Java evaluates && and || left to right, and it stops as soon as it knows the answer. In a && b, if a is false, the whole thing must be false, so Java never evaluates b. In a || b, if a is true, the whole thing must be true, so Java never evaluates b. This is called short-circuit evaluation.

Short-circuiting is useful: put a guard on the left that protects the right. n != 0 && 100 / n > 5 never divides by zero, and s.length() > 0 && s.substring(0, 1).equals("A") never asks for a character that isn't there. Swap the two sides and the same code can crash.

1int n = 0;2String s = "";3if (n != 0 && 100 / n > 5)14{5    System.out.println("big share");6}7if (s.length() == 0 || s.substring(0, 1).equals("A"))28{9    System.out.println("empty or starts with A");10}11System.out.println(n == 0 || 100 / n > 5);3
  1. line 3 Left side false: the division never happens
  2. line 7 Left side true: substring is never called
  3. line 11 Same idea in a println: no exception
output › empty or starts with A true
Guards on the left, risky work on the right, run under Java 17. No exception is ever thrown.
?Wait, why would 100 / n crash?
If n is 0, 100 / n divides an int by zero, which throws an ArithmeticException (Unit 1). Short-circuiting means that with the guard n != 0 on the left of &&, that division is skipped whenever n is 0.
Pause and think
For an int x, when is x > 3 && x < 3 true? When is x > 3 || x < 8 true?
I thought about it. Show me
x > 3 && x < 3 is never true: no number is both bigger and smaller than 3. x > 3 || x < 8 is always true: any number that isn't bigger than 3 is certainly smaller than 8. Watch for conditions like these: they look reasonable and do nothing useful.
Precedence and short-circuits

With boolean p = true, boolean q = false and int x = 6, evaluate each expression.

(a) !p || q && x > 5
(b) p || q && x < 0
(c) !(p && x % 4 == 2)
  1. (a) ! applies only to p, and && comes before ||: it's (!p) || (q && x > 5). !p is false. q && x > 5: q is false, so && short-circuits to false. false || false is false.
    Why?
    Because q is false, Java never even evaluates x > 5.
  2. (b) It groups as p || (q && x < 0). p is true, so || short-circuits: the whole expression is true and the right side is never evaluated.
  3. (c) Inside the parentheses first: x % 4 is 2, so x % 4 == 2 is true. p && true is true. Then ! flips it: false.

Answer: (a) false (b) true (c) false

Stuck? Get a hint
  1. Add the invisible parentheses first: ! sticks to the value right after it, and && groups before ||.
  2. For &&, a false left side ends it. For ||, a true left side ends it.
  3. In (c), the parentheses force && to happen before the !.
Check yourself
Assume str is a String that may be empty. Which condition is safe for every value of str and is true exactly when str starts with "x"?
  1. Astr.substring(0, 1).equals("x") && str.length() > 0
  2. Bstr.length() > 0 && str.substring(0, 1).equals("x")
  3. Cstr.length() > 0 || str.substring(0, 1).equals("x")
  4. Dstr.length() >= 0 && str.substring(0, 1).equals("x")
Show the answer

B. With the length check on the left, && skips the substring call whenever str is empty. Choice A has the right pieces in the wrong order: for "", substring(0, 1) runs first and throws an exception. Choice C is true for every nonempty String, and D's guard is always true, so it doesn't protect anything.

  • && needs both true. || needs at least one. ! flips.
  • Precedence: !, then &&, then ||. Relational operators come before all three.
  • Ranges: x >= lo && x <= hi. Outside a range: x < lo || x > hi.
  • Short-circuit: a false left side ends &&, a true left side ends ||. Put the guard on the left.