Skip to main content

Unit 2 · Topic 2.4

2.4 Nested if Statements

Some decisions have more than two outcomes, or depend on an earlier decision. This topic covers nested if statements and if-else-if chains, which give multiway selection, and why the order of the conditions in a chain matters so much.

Key terms

  • nested if
  • if-else-if
  • multiway selection
  • trailing else

Nested if statements

A nested if is an if (or if-else, or a whole chain) placed inside the body of another one. The inner condition is checked only if the outer condition was true. If the outer one is false, Java never even looks at the inner one.

int age = 15; boolean hasTicket = true; if (age >= 13) { if (hasTicket) { System.out.println("Enter"); } else { System.out.println("Buy a ticket"); } } else { System.out.println("Too young"); }

Here the ticket is checked only for people 13 or older. With age 15 and a ticket, this prints Enter. With age 10, the outer condition is false, so it prints Too young and hasTicket is never checked.

Multiway selection with if-else-if

An if-else-if chain tests conditions in order, from the top. As soon as one is true, Java runs that body and skips the rest of the chain. At most one body runs. If none of the conditions is true and there's a trailing else at the end, its body runs; with no trailing else, nothing in the chain runs.

public static String letter(int score) { if (score >= 90) { return "A"; } else if (score >= 80) { return "B"; } else if (score >= 70) { return "C"; } else { return "F"; } }

A score of 85 fails the first test and passes the second, so it returns "B". Java never reaches score >= 70, even though 85 passes that too. That's why the chain can use simple conditions like score >= 80 instead of score >= 80 && score < 90: anything 90 or above was already caught.

Order matters

A chain is really just nested if-else statements written in a tidy way: each else if is an if inside the previous else. Because the first true condition wins, a chain with its conditions in the wrong order can be badly wrong. If the test for score >= 70 comes first, a 95 matches it and gets a C. When the conditions overlap, put the most restrictive one first.

Also compare a chain with separate if statements. Separate ifs are each checked on their own, so several bodies can run:

int n = 12; if (n > 5) { System.out.println("big"); } if (n > 10) { System.out.println("huge"); }

This prints both big and huge. Written as if ... else if ..., it would print only big.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Conditions in the wrong order

    A student wrote this to assign letter grades. What is printed, and what's wrong?int score = 95; String grade = "F"; if (score >= 70) { grade = "C"; } else if (score >= 80) { grade = "B"; } else if (score >= 90) { grade = "A"; } System.out.println(grade);

    Show the solution
    1. Step 1: Java checks the conditions from the top. 95 >= 70 is true, so grade becomes "C".
    2. Step 2: Since one branch already ran, the rest of the chain is skipped. score >= 90 is never checked.
    3. Step 3: Every passing score, even 100, lands in the first branch. The fix is to test from the highest cutoff down: >= 90, then >= 80, then >= 70.

    Answer: It prints C. The conditions are in the wrong order, so an A score is caught by the C test first.

  2. Example 2

    Tracing a nested if

    What does this code print?int age = 10; boolean hasTicket = true; if (age >= 13) { if (hasTicket) { System.out.println("Enter"); } else { System.out.println("Buy a ticket"); } } else { System.out.println("Too young"); }

    Show the solution
    1. Step 1: The outer condition is age >= 13, which is 10 >= 13, so it's false.
    2. Step 2: Java skips the whole outer if body, including the nested if-else inside it. hasTicket doesn't matter.
    3. Step 3: The outer else runs.

    Answer: It prints Too young.

Common mistakes

  • Putting a broad condition first in an if-else-if chain. The first true condition wins, so it hides the narrower ones below it.
  • Thinking more than one branch of a chain can run. At most one does, and exactly one if there's a trailing else.
  • Mixing up a chain with separate if statements, where every true condition runs its body.

On the exam

  • Questions often ask which inputs make a chain print a certain result. Try the boundary values, like exactly 80 or 90.
  • When you write a chain on a free-response question, you can rely on earlier conditions being false. You don't need to repeat them.

Connected topics

Videos

  • AP Computer Science A - Topic 2.4 - Part 1: Nested If Statements

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CSA Selection and Iteration – Nested Conditionals

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • if else if Statements

    CodeHSWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 2.4 - Part 2: Nested If Statements

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Nested if, else if, and else (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

Check yourself

4 questions on 2.4 Nested if Statements. Pick an answer to see if you got it, and why.

Question 1 of 4

Consider the following method.public static String grade(int score) { String result; if (score >= 70) { result = "C"; } else if (score >= 80) { result = "B"; } else if (score >= 90) { result = "A"; } else { result = "F"; } return result; }The following code segment appears in another method of the same class.System.out.println(grade(95) + " " + grade(65));What is printed as a result of executing the code segment?

Question 2 of 4

Consider the following code segment.int x = 15; String s = ""; if (x > 10) { s += "A"; } if (x > 5) { s += "B"; } else { s += "C"; } if (x > 20) { s += "D"; } else if (x > 12) { s += "E"; } else { s += "F"; } System.out.println(s);What is printed as a result of executing the code segment?

Question 3 of 4

Consider the following method.public static String sortIt(int a, int b) { if (a > 3) { if (b < 10) { return "X"; } else { return "Y"; } } else { return "Z"; } }The following code segment appears in another method of the same class.System.out.println(sortIt(5, 12) + sortIt(5, 8) + sortIt(2, 8));What is printed as a result of executing the code segment?

Question 4 of 4

Assume a and b are boolean variables and x is an int variable. Consider the following code segment.if (a) { if (b) { x++; } }Which of the following code segments has the same effect for all values of a and b?

0 of 4 answered