How does an if-else statement choose between two alternative blocks of code?
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.
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.3) wants you to write and trace a two-way if-else statement: a structure that chooses between two blocks of code. When the boolean condition is true, the if block runs; when it is false, the else block runs. Exactly one of the two always runs - never both, never neither. This is the standard tool for either/or decisions.
Syntax of if-else
if (condition) {
// block A: runs when condition is true
} else {
// block B: runs when condition is false
}
// execution continues here afterwards
The else keyword has no condition of its own. It simply provides the alternative path taken whenever the if condition evaluates to false.
Exactly one branch runs
You can visualize the choice:
The else attaches to the nearest if
Because of this rule, careful bracing matters whenever you nest an if inside another if. Writing each block with braces guarantees the else attaches where you intend.
Nested if-else
You can place an if-else inside the if or else block of another, to make a decision in stages. For long chains of mutually exclusive options, the else if form (Topic 3.4) is cleaner, but a single nested if-else is common for two-level decisions. When you nest, brace every block: this both removes the dangling-else ambiguity and makes it obvious which condition each branch belongs to. The outer decision is made first, and only the chosen branch's inner if-else is then evaluated, so the two decisions happen in sequence rather than at once.
if (temp > 30) {
System.out.println("hot");
} else {
if (temp < 10) {
System.out.println("cold");
} else {
System.out.println("mild");
}
}
Try this
Q1. In an if-else, how many of the two blocks run on a single pass? [1 point]
- Cue. Exactly one - the
ifblock if the condition istrue, otherwise theelseblock.
Q2. Explain the difference between one if-else and two separate if statements with opposite conditions. [2 points]
- Cue.
if-elseguarantees exactly one branch runs and evaluates the condition once; two separateifstatements evaluate two conditions and, if written carelessly, could both run or both be skipped.
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 x = 5;
int y = 0;
if (x > 10) {
y = 1;
} else {
y = 2;
}
y = y + 3;
System.out.println(y);
```
What is printed?
(A) `5`
(B) `4`
(C) `1`
(D) `2`
(E) `3`
Show worked answer →
The answer is (A).
The condition x > 10 is 5 > 10, which is false, so the else block runs and y becomes 2. The if block (y = 1) is not executed; exactly one of the two branches runs. After the if-else, y = y + 3 runs unconditionally: 2 + 3 = 5. (C) and (D) forget the trailing + 3; (B) wrongly takes the if branch.
Markers reward knowing that exactly one branch of an if-else executes, and that code after the if-else runs in every case.
AP 2019 (style)3 marksFree response (code writing). An `int` variable `num` is given. Write a code segment that prints `"even"` if `num` is divisible by 2 and `"odd"` otherwise, using a single if-else statement. Print exactly one of the two words.
Show worked answer →
A 3-point question testing two-way selection.
if (num % 2 == 0) {
System.out.println("even");
} else {
System.out.println("odd");
}
Point 1: the condition num % 2 == 0 correctly tests divisibility by 2 using modulo. Point 2: the if block prints "even" when the remainder is 0. Point 3: the else block prints "odd" in every other case. Because it is if-else, exactly one word is printed - never both, never neither. Using two separate if statements without else would risk printing nothing or duplicating logic.
Related dot points
- 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.
- 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.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.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 3.7 Comparing Objects: compare object references with == and !=, compare object contents with equals, and detect a null reference, understanding the difference between identity and equality.
A focused answer to AP CSA Topic 3.7, covering reference equality with == and !=, content equality with the equals method, comparing against null, why two equal-looking objects can be unequal by ==, and using equals/compareTo for Strings, with a fully worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)