Nested if Statements
Introduced| English | Meaning |
|---|---|
| nested if/ˈnestɪd ɪf/ | nested if |
A broad first branch hides the top grade
- A grade ladder tests
s >= 60beforeelse if (s >= 90). With s=95, the first condition is true, so its branch runs and the top-grade branch is never tested. - For these overlapping lower bounds, test the higher threshold first. This is a reasoning rule for this ladder, not a requirement to sort every possible condition from “most specific” to “least specific”.
Nesting makes a decision depend on its enclosing path
- A nested if places an if inside another branch. If the inner statement is in the outer true branch, it is reached only when that outer condition is true; an if inside an else is reached on the false path instead.
- Use braces to show that structure. An else without braces associates with the nearest unmatched if, which can differ from what indentation suggests.
A ladder chooses its first matching branch
- An
if ... else if ... elseladder checks conditions from top to bottom until the first true one. With a final else, one branch is selected when condition evaluation completes; without an else, none may run. - The method below accepts ordinary integer scores and returns A, B, C or F. For 85 it checks 90, then 80, returns B, and never evaluates the remaining branch tests.
public class GradeChoice {
public static String grade(int s) {
if (s >= 90) {
return "A";
} else if (s >= 80) {
return "B";
} else if (s >= 70) {
return "C";
} else {
return "F";
}
}
}
Distinguish overlap from disjoint cases
- With overlapping lower bounds,
s >= 60includes every score satisfyings >= 90. Putting it first prevents the second branch from being selected, even though Java can compile the ladder. - For pure disjoint tests such as
x < 0,x == 0andx > 0, changing their order while retaining the same outcomes need not change the result. Overlapping policies may instead deliberately prioritize one case; follow the stated requirement.
The first true branch wins
s = 85 takes the >= 80 branch (B); later branches are skipped.
In an else-if ladder, which condition's block runs?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Java takes the first true condition top to bottom.
For if (s>=90) A; else if (s>=80) B; else C; with s = 85, the result is...
Difficulty 1/5 · 100 coins + a small bonus on first completion today
85 fails >=90 but passes >=80 → B, then stop.
If a broad condition comes before a narrower one in a ladder, the narrower branch can become unreachable.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
The broad condition catches those cases first; put tightest first.
For an inner if placed inside the outer if's true branch, the inner condition is checked even when the outer condition is false.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
That inner statement is not reached on the false path. An if placed inside an else branch would have a different enclosing path.
For the shown grade ladder using s >= 90, s >= 80 and s >= 70, which ordering preserves the intended top grades?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Higher lower bounds come first because these comparisons overlap. This is not a universal ordering rule for all Boolean conditions.
Separate independent ifs from a ladder
- Two separate statements,
if (s >= 60) ...; if (s >= 90) ...;, both run their bodies for s=95. An else-if ladder links the alternatives and chooses just its first match. - Trace with actual values at the threshold and just below it: 90 goes to A, 89 to B, 80 to B, 79 to C and 69 to F. Testing only 85 would miss a wrong boundary operator.
“First true wins” describes the linked ladder, not multiple independent if statements. A branch can be impossible to select because an earlier comparison catches all its inputs without causing a compile-time unreachable-statement error.
Two independent if statements test s >= 60 and s >= 90. For s=95, how many bodies run?
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Both conditions are true and the statements are independent, so both bodies run.
Trace the false branch of an outer if
- In
if (weekday) { ... } else { if (raining) { ... } }, the inner raining test is reached when weekday is false. The enclosing branch, not the mere word “nested”, determines when it runs. - Identify braces, linked alternatives, overlapping conditions and the actual input before predicting a path. Keep the code's policy and the boundary tests consistent; indentation alone does not determine Java's grouping.
A nested if runs only when control reaches its enclosing branch. A linked ladder selects the first true condition; independent if statements can run several bodies. Higher thresholds come first in this particular grade ladder because its conditions overlap.
With if (s >= 60) result = 1; else if (s >= 90) result = 2; else result = 3;, what is result for s = 95?
Difficulty 2/5 · 140 coins + a small bonus on first completion today
95 satisfies the first comparison, so result becomes 1 and the else-if branch is skipped.
An if inside an outer else branch can be reached when the outer condition is false.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Yes: the outer false path enters else, where the inner test may run.