Several cases in a row
After this: Write a chain of cases and put them in the right order.
On this page · 6 sections · 3 checks
A teacher turns scores into grades with a rule: 90 or more is an A, 80 or more is a B, 70 or more is a C, and anything else is a D. That is four possible answers. One if and one else give only two paths, so we need something more.
One question after another
The idea is to ask the questions in a row. Ask "90 or more?" first. If yes, print A and stop. If no, ask "80 or more?". If yes, print B and stop. If no, ask "70 or more?". If every question is answered no, the else part catches the score and prints D.
Work through it with score = 72.
- Is 72 at least 90? No. Move on to the next question.
- Is 72 at least 80? No. Move on again.
- Is 72 at least 70? Yes. Print C, and skip everything after it.
The rule: in a chain, the questions are asked from the top. The first one answered yes wins. Its lines run, and the rest of the chain is skipped. When every answer is no, the else part runs.
The code
Python spells the extra questions elif, which is short for else if.Java spells the extra questions else if, two words. Each one is asked only if every question above it was answered no.
In the trace, each no shows up as a jump. Count two jumps, then one print.
- Run the programRun it and count the jumps. Each jump is a question answered no. The chain stops at the first yes.
score = 72if score >= 90: print("A")elif score >= 80: print("B")elif score >= 70: print("C")else: print("D")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Change the score to 85 and run again, then to 50. Each time, find the first question that is answered yes.
Why the order matters
Suppose someone writes the same chain from the bottom: "70 or more?" first, then 80, then 90. Now give it a score of 95.
Is 95 at least 70? Yes. Print C. The chain stops. The questions about 80 and 90 are never asked, even though 95 would answer yes to both. A score that deserves an A gets a C.
score = 95if score >= 70: print("C")elif score >= 80: print("B")elif score >= 90: print("A")else: print("D")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The order matters because the cases overlap: 95 is at least 70, at least 80 and at least 90. The chain does not look for the best match. It takes the first yes. So when cases overlap, put the most demanding question first, and the least demanding last.
A chain is one decision
A chain, however long, is one decision with several outcomes. Three separate if lines are three decisions, and each is asked no matter what the others answered. For 95, that prints all three letters.
score = 95if score >= 90: print("A")if score >= 80: print("B")if score >= 70: print("C")A B C
int score = 95;if (score >= 90) { System.out.println("A");}if (score >= 80) { System.out.println("B");}if (score >= 70) { System.out.println("C");}A B C
When you want one answer out of several, use a chain.
Check yourself
What does this print?
score = 85if score >= 90: print("A")elif score >= 80: print("B")elif score >= 70: print("C")else: print("D")int score = 85;if (score >= 90) { System.out.println("A");} else if (score >= 80) { System.out.println("B");} else if (score >= 70) { System.out.println("C");} else { System.out.println("D");}The chain written from the bottom asks score >= 70 first, then >= 80, then >= 90. A score of 95 prints C. Why?
The B case is missing: 80 or more is a B. Write the whole line, starting with elif} else if.
score = 85if score >= 90: print("A")____________________ # ← the B case print("B")elif score >= 70: print("C")else: print("D")int score = 85;if (score >= 90) { System.out.println("A");____________________ // ← the B case System.out.println("B");} else if (score >= 70) { System.out.println("C");} else { System.out.println("D");}Honesty notes
- Write a chain of cases (elif in Python, else if in Java) that ends with an else.
- Say why only one case of a chain runs: the first question answered yes wins and the rest are skipped.
- Put overlapping cases in the right order, with the most demanding question first.
- Tell a chain from three separate ifs.