How does an if statement use a boolean condition to control which statements run?
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.
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.2) wants you to write and trace a one-way if statement: a block of code that runs only when its boolean condition is true, and is skipped when the condition is false. After the if, control continues with whatever follows. Understanding exactly which statements the if controls - and what runs regardless - is the core skill, and a favorite source of multiple-choice traps.
Syntax of the one-way if
if (condition) {
// statements that run only when condition is true
}
// execution continues here in every case
The condition in parentheses must evaluate to a boolean. When it is true, the statements inside the braces (the body or block) execute. When it is false, the body is skipped entirely. Either way, execution then continues with the statement after the closing brace.
Why braces matter
Consider:
if (x > 0)
System.out.println("positive");
System.out.println("done"); // NOT controlled by the if
The second println runs whether or not x > 0, because only the first statement belongs to the braceless if. Adding braces makes the intent explicit and prevents this classic bug.
Flow of control
You can visualize the branch:
The condition is a boolean
The parentheses must hold a boolean. That can be a relational expression (x > 0), a boolean variable (isReady), or a compound expression (a > 0 && b < 10, Topic 3.5). You never compare a boolean to true explicitly: write if (isReady), not if (isReady == true). The condition is evaluated exactly once, at the moment control reaches the if; its value at that instant decides whether the body runs. If variables inside the condition change later in the body, that does not re-evaluate the condition - a one-way if tests once and never loops back, unlike the loops of Unit 4.
Try this
Q1. An if has no braces. How many statements does it control? [1 point]
- Cue. Exactly one - the single statement immediately following the condition.
Q2. Explain why if (ready == true) is poor style and what to write instead. [2 points]
- Cue.
readyis already aboolean, so comparing it totrueis redundant; writeif (ready). (And the analogousif (ready == false)should beif (!ready).)
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 2022 (style)1 marksMultiple choice. Consider the following code segment.
```java
int n = 4;
if (n % 2 == 0)
n = n + 10;
n = n + 1;
System.out.println(n);
```
What is printed?
(A) `15`
(B) `5`
(C) `14`
(D) `4`
(E) `25`
Show worked answer →
The answer is (A).
Without braces, an if controls only the single statement that follows it. The condition n % 2 == 0 is 4 % 2 == 0, which is true, so n = n + 10 runs, making n 14. The second line, n = n + 1, is not part of the if (the indentation is misleading); it always runs, making n 15. (C) forgets the + 1; (B) wrongly skips the + 10; (D) and (E) misread control flow.
Markers reward knowing that a braceless if governs exactly one statement, and that indentation does not determine control flow in Java.
AP 2020 (style)3 marksFree response (code writing). An `int` variable `score` is given. Write a code segment that adds 5 bonus points to `score` only when `score` is at least 90, and then in all cases prints the final value of `score` on its own line. Use a single if statement with a block.
Show worked answer →
A 3-point question testing a one-way if and control flow.
if (score >= 90) {
score = score + 5;
}
System.out.println(score);
Point 1: the boolean condition score >= 90 correctly uses >= for "at least 90". Point 2: the body adds 5 to score, enclosed in braces so it is the controlled block. Point 3: the println sits outside the if so it runs in every case. A common error is putting the println inside the braces, which would skip printing when the condition is false.
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.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.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.
- Topic 2.3 Calling a Void Method: call a non-static void method on an object using dot notation, and explain that a void method performs an action but returns no value.
A focused answer to AP CSA Topic 2.3, covering dot notation for calling methods on objects, what void means, why a void call cannot be used in an expression, the difference between a method signature and a call, and method side effects, with a worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)