Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.810 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. Syntax of the one-way if
  3. Why braces matter
  4. Flow of control
  5. The condition is a boolean
  6. Try this

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:

flowchart TD A[evaluate condition] -->|true| B[run if body] A -->|false| C[skip body] B --> D[continue after if] C --> D

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. ready is already a boolean, so comparing it to true is redundant; write if (ready). (And the analogous if (ready == false) should be if (!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

Sources & how we know this