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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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 isfalse.false || false == false; every other||combination istrue.!true == false;!false == true.
Precedence
When several logical operators appear together, precedence is:
!(highest)&&||(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 > 5isfalse, so!(false)istrue;true || anythingistrue(and2 == 2is alsotrue).
Q2. Explain how && short-circuiting lets you safely write index < arr.length && arr[index] > 0. [2 points]
- Cue. The left operand
index < arr.lengthis evaluated first; if it isfalse,&&short-circuits andarr[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
- Topic 3.1 Boolean Expressions: evaluate expressions formed with the relational operators (<, >, <=, >=, ==, !=) that produce a boolean result of true or false.
A focused answer to AP CSA Topic 3.1, covering the six relational operators, how they compare numeric primitives to produce a boolean, the difference between == and =, comparing doubles, and how relational operators combine with arithmetic, with a fully traced worked evaluation.
- Topic 3.6 Equivalent Boolean Expressions: apply De Morgan's laws and truth tables to produce equivalent boolean expressions and to simplify negations of compound conditions.
A focused answer to AP CSA Topic 3.6, covering De Morgan's laws for negating && and ||, using truth tables to prove equivalence, simplifying double negation, and rewriting conditions to remove a leading !, with a fully worked example.
- Topic 3.3 if-else Statements: use a two-way if-else statement so that exactly one of two code blocks runs depending on whether the boolean condition is true or false.
A focused answer to AP CSA Topic 3.3, covering two-way selection with if-else, the guarantee that exactly one branch runs, how the else attaches to the nearest if, nested if-else, and how to trace each branch, with a fully worked example.
- Topic 3.4 else if Statements: use an if / else if / else chain so that the first true condition runs its block and the rest are skipped, selecting exactly one outcome.
A focused answer to AP CSA Topic 3.4, covering the if / else if / else chain, why the first true condition wins and later ones are skipped, why ordering matters, the role of the final else, and how to trace a multi-branch decision, with a fully worked example.
- Topic 3.2 if Statements and Control Flow: use a one-way if statement so that a block of code runs only when its boolean condition is true, and trace the resulting flow of control.
A focused answer to AP CSA Topic 3.2, covering the syntax of a one-way if statement, the role of the boolean condition, why braces matter, the dangling-statement trap, and how control flows through and past the if, with a fully traced worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)