Conditional Statements and Nested if

AP Computer Science A · Unit 2 — Selection and Iteration

if, else if, else

An if statement tests a boolean expression and runs its body only when the expression is true. Chaining with else if builds a decision ladder: Java tests the conditions in order and executes the body of the first one that is true, then skips every remaining branch. A final else runs when no condition matched.

Because the ladder stops at the first match, the conditions in a range ladder must be written from the most restrictive to the least restrictive. A ladder that starts with the widest test can never reach the branches below it, and the compiler reports nothing at all: the code is legal, it simply always answers the same way.

Nested conditionals

An if may appear inside the body of another if. The inner test is reached only when the outer test is true, so a nested pair expresses a logical and. In fact if (p) { if (q) { S } } and if (p && q) { S } behave identically. The two forms stop being interchangeable as soon as either level has an else: an else always belongs to the nearest unmatched if, no matter how the code is indented.

n % 15 == 0n % 3 == 0n % 5 == 0"FizzBuzz""Fizz""Buzz"else: ntruetruetruefalsefalsefalseSwap the top two boxes: the "FizzBuzz" branch never runs, yet the code still compiles.
Read the figure along the "false" arrows. The vertical column on the left is the order in which Java checks the conditions; each horizontal arrow is a branch that has been chosen and exits the ladder completely. Because n % 15 == 0 is the most restrictive condition, it must be at the top. If you put n % 3 == 0 first, every multiple of 15 stops right there and returns "Fizz" — the "FizzBuzz" branch becomes dead code. No warning is issued; the program runs smoothly and gives the wrong answer.

Worked example: a branch that never runs

The following code is meant to classify a positive integer n: return 1 if it is divisible by 6, return 2 if it is divisible by 2, return 3 if it is divisible by 3, otherwise return 4. Point out where it goes wrong and fix it.

int r;
if (n % 2 == 0) {
    r = 2;
} else if (n % 3 == 0) {
    r = 3;
} else if (n % 6 == 0) {
    r = 1;
} else {
    r = 4;
}

Solution

Find the error. Take n = 12, a multiple of 6. Java checks the first condition: 12 % 2 == 0 is true, so it assigns r = 2 and exits the whole ladder. The n % 6 == 0 branch is never reached, because every multiple of 6 is an even number and so is caught by the first branch.

Why the compiler stays silent. The code is syntactically valid; a "branch that never runs" is a logic error, not a syntax error. Only reading the order reveals it.

Fix. Move the most restrictive condition to the top:

int r;
if (n % 6 == 0) {
    r = 1;
} else if (n % 2 == 0) {
    r = 2;
} else if (n % 3 == 0) {
    r = 3;
} else {
    r = 4;
}

Now n = 12 gives 1, n = 8 gives 2, n = 9 gives 3, n = 7 gives 4 — all four groups are correct.

Common mistake

The costliest error of the chapter: arranging an else if ladder from the broad condition down to the narrow one, which turns the lower branches into dead code. It produces no compile error and no exception, only silently wrong answers. How to prevent it: before writing the ladder, list one representative value for each branch, then trace by hand from the top down — any branch that no value falls into is a dead branch and must be moved up.

Key takeaway

An else if ladder stops at the first true branch, so order is part of the logic. Put the narrow conditions on top. And an else always belongs to the nearest if that does not yet have an else, not the one the indentation suggests.

← Boolean Expressions and De Morgan's Laws · All AP Computer Science A lessons · Practice AP Computer Science A questions (free, instant scoring) · while Loops, for Loops, and Counting Iterations →