2.5 else if Chains
else if Chains and Condition Order in Java
Unit 2: Variables, Decisions, and Input · Lesson 2.5 · about 25 minutes · no coding experience needed
Two choices was easy. Real games have five: which key was pressed, which level you are on, which band your score falls into. A chain handles that, and it has one trap that will quietly ignore half the code you wrote.
What you will be able to do
- Write an else if chain that picks exactly one branch
- Explain why the order of conditions changes the result
- Spot a branch that can never be reached
Words you will need
- else if chain
- In plain words A list of questions, checked from the top, stopping at the first yes.
- More precisely A sequence of conditions where at most one branch executes.
- Unreachable branch
- In plain words Code that can never run because something above it always catches first.
- More precisely A branch whose condition is subsumed by an earlier one.
Build it step by step
Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.
-
Step 1
On your screen An else if chain with three branches shown in an editor.
if (score >= 90) { grade = "A"; } else if (score >= 80) { grade = "B"; } else { grade = "C"; }Java checks from the top and STOPS at the first condition that is true. Once a branch is taken, the rest are not even looked at.
That is why `score >= 80` does not need to also say "and less than 90". If the score were 95 we would never have reached this line.
-
Step 2
On your screen The same chain with the conditions in the wrong order, and the unreachable branches graded.
// WRONG. Everything after the first branch is unreachable. if (score >= 60) { grade = "D"; } else if (score >= 90) { grade = "A"; // can never run }Same conditions, different order, completely broken.
A score of 95 is greater than 60, so the first branch catches it and the A branch is never reached. Nobody can ever get an A.
This compiles without a murmur. The rule that saves you: in a chain of ranges, put the NARROWEST condition first and let the chain widen as it goes down.
-
Step 3
On your screen A chain with no final else, showing a value that matches nothing.
if (key == 1) { move(4); } else if (key == 2) { move(-4); }A chain does not have to end in else. But then it is possible for NOTHING to run.
That is fine when doing nothing is the right answer. It is a bug when you assumed one of the branches would always catch. Ask yourself which you meant.
Mistakes almost everyone makes here
These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.
Common wrong idea Every condition in the chain is checked.
What is actually true Checking stops at the first true one.
Why it matters This is what lets later conditions be written simply, and what makes order matter.
Common wrong idea The order of the branches does not matter as long as the conditions are right.
What is actually true Order decides which branch catches. A broad one first hides everything below.
Why it matters The most common logic bug in the unit, and it produces no error at all.
Common wrong idea A chain always runs something.
What is actually true Without a final else, it is possible for no branch to run.
Why it matters Silently doing nothing is very hard to debug if you expected a default.
Common wrong idea Separate ifs behave the same as an else if chain.
What is actually true Separate ifs are all checked, so several can run.
Why it matters This is the difference between one arrow key moving you and two moving you twice.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. score is 95. Which grade? if (score >= 90) grade = "A"; else if (score >= 80) grade = "B";
-
2. In this chain, when can the second branch ever run? if (n > 0) { ... } else if (n > 10) { ... }
-
3. How many branches of an else if chain can run at once?
-
4. When ordering a chain of number ranges, which should come first?
Fill in the code
Give a rank based on coins collected: 20 or more is "gold", 10 or more is "silver", and anything else is "bronze". Put the conditions in an order that actually works.
if (coins >= )
{
rank = "gold";
}
(coins >= )
{
rank = "silver";
}
else
{
rank = "bronze";
}
Stuck? Open a hint for each blank
- Blank 1: The narrowest band has to be tested first. Which number is hardest to reach?
- Blank 2: Two keywords, to continue the chain rather than start a new decision.
- Blank 3: The middle band.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. Java checks the conditions in a chain:
-
2. n is 50. Which branch runs? if (n > 10) { a(); } else if (n > 40) { b(); } else { c(); }
-
3. What is the difference between two separate ifs and an else if chain?
-
4. A chain has no final else. What can happen?
The short version
- A chain is checked top down and stops at the first true condition.
- At most one branch runs.
- Order decides everything. Narrowest condition first.
- No final else means it is possible for nothing to run.
If you get stuck
These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]