Unit 2 Cycle 1 Day 6: Multi-Branch Else-If

Unit 2 Foundation (Cycle 1) Day 6 of 28 Foundation

Multi-Branch Else-If

Section 2.4 — Else-If Statements

Key Concept

An else-if chain evaluates conditions from top to bottom and executes only the first matching branch. Once a condition is true, all remaining else-if and else blocks are skipped entirely. This means the order of conditions matters: placing a broader condition before a narrower one can prevent the narrower condition from ever being reached. The AP exam tests whether rearranging conditions changes the output, and whether you understand that at most one branch in the chain executes.

Consider the following code segment.

int temp = 75; String msg; if (temp > 100) { msg = "extreme"; } else if (temp > 85) { msg = "hot"; } else if (temp > 70) { msg = "warm"; } else { msg = "cool"; } System.out.println(msg);

What is printed as a result of executing the code segment?

Answer: (C) warm

temp = 75. Check: 75 > 100 false. Check: 75 > 85 false. Check: 75 > 70 true. Assigns "warm" and skips the else.

Why Not the Others?

(A) 75 is not greater than 100.

(B) 75 is not greater than 85.

(D) 75 IS greater than 70, so the else branch is never reached.

Common Mistake

In an else-if chain, only the FIRST true condition executes. Once a match is found, all remaining branches are skipped. The order of conditions matters.

AP Exam Tip

Else-if chains should be ordered from most restrictive to least restrictive (or vice versa, depending on logic). Each condition assumes all previous conditions were false.

Review this topic: Section 2.4 — Else-If Statements • Unit 2 Study Guide

More Practice

Related FRQs

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.