Skip to main content
United StatesComputer ScienceSyllabus dot point

How do the logical operators &&, || and ! combine conditions, and what is short-circuit evaluation?

Topic 3.5 Compound Boolean Expressions: combine boolean expressions with the logical operators && (and), || (or) and ! (not), applying short-circuit evaluation.

A focused answer to AP CSA Topic 3.5, covering the logical operators && , || and !, their truth tables, operator precedence and short-circuit evaluation, why short-circuiting prevents errors, and how to trace a compound condition, with a fully worked example.

Generated by Claude Opus 4.811 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. The three logical operators
  3. Precedence
  4. Short-circuit evaluation
  5. Try this

What this topic is asking

The College Board (Topic 3.5) wants you to combine boolean expressions with the logical operators: && (logical AND), || (logical OR) and ! (logical NOT). You need their truth tables, their precedence relative to each other and to relational operators, and - critically - short-circuit evaluation, the rule that Java stops evaluating a compound expression as soon as the result is determined. Short-circuiting is both an efficiency feature and a common exam trap.

The three logical operators

Truth tables:

  • true && true == true; every other && combination is false.
  • false || false == false; every other || combination is true.
  • !true == false; !false == true.

Precedence

When several logical operators appear together, precedence is:

  1. ! (highest)
  2. &&
  3. || (lowest)

Relational operators (<, >, ==, and so on) are evaluated before any logical operator. So a > 0 && b < 10 means (a > 0) && (b < 10). When mixing && and ||, the && groups bind first: p || q && r means p || (q && r). Parentheses make the grouping explicit and are encouraged.

Short-circuit evaluation

This is why the order of operands matters. The standard pattern guards a risky operation by testing the safety condition first:

if (x != 0 && y / x > 1) {
  // y / x is only evaluated when x != 0, so no division by zero
}

If x is 0, x != 0 is false, and && short-circuits before y / x runs. Reversing the operands would defeat the guard.

Try this

Q1. State the value of !(3 > 5) || (2 == 2). [1 point]

  • Cue. true - 3 > 5 is false, so !(false) is true; true || anything is true (and 2 == 2 is also true).

Q2. Explain how && short-circuiting lets you safely write index < arr.length && arr[index] > 0. [2 points]

  • Cue. The left operand index < arr.length is evaluated first; if it is false, && short-circuits and arr[index] is never accessed, avoiding an out-of-bounds error.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2021 (style)1 marksMultiple choice. Consider the following code segment. ```java int a = 5; int b = 0; boolean result = (b != 0) && (a / b > 2); System.out.println(result); ``` What is printed? (A) `false` (B) `true` (C) Nothing; an ArithmeticException (division by zero) occurs. (D) Nothing; a compile-time error occurs. (E) `0`
Show worked answer →

The answer is (A).

With &&, Java evaluates the left operand first. b != 0 is 0 != 0, which is false. Because false && anything is false, Java short-circuits: it does not evaluate the right operand a / b > 2, so the division by zero never happens. The whole expression is false. (C) is exactly the trap short-circuiting avoids; (D) is wrong because the code compiles.

Markers reward knowing that && short-circuits when the left operand is false, which is the standard way to guard against division by zero.

AP 2019 (style)3 marksFree response (code writing). Two `int` variables `age` and `years` are given. Write a code segment that assigns to a `boolean` variable `eligible` the value `true` when the person is at least 18 **and** has at least 3 years of experience, **or** is at least 65 regardless of experience, and `false` otherwise.
Show worked answer →

A 3-point question testing compound conditions with && and ||.

boolean eligible = (age >= 18 && years >= 3) || (age >= 65);

Point 1: age >= 18 && years >= 3 requires both conditions, using &&. Point 2: combining that with the senior path using ||, which is true when either side is true. Point 3: parenthesising the && group so it is grouped before the || (though && already binds tighter, the parentheses make the intent unambiguous and are good practice). Storing the result in eligible. A common error is reversing the precedence assumption between && and ||.

Related dot points

Sources & how we know this