Unit 2 · Programming 1 · Lesson 2.13

elif and Nested Conditionals

Two doors isn’t always enough. Today: how to sort a value into many buckets with elif, and how to read conditionals inside conditionals — a top-three hardest question type on the exam, so we start the drill now.

Big Idea 3 — Algorithms & ProgrammingThe hard-trace drill starts today

Before this lesson — read on Runestone

Shows why a chain of elif beats a pile of separate ifs. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Still in Programs. In 2.12 an if chose between two paths. Real problems often need many paths — a letter grade has five, a shipping speed has three. elif is how you chain conditions so exactly one wins.

2

The Hook

Grade this: 85. Is that an A? A B? Your brain checked “90 or up?” — no — “80 or up?” — yes, stop, it’s a B. You didn’t keep checking after you found the answer.

That “check in order, stop at the first match” behavior is exactly what an elif chain does — and it’s why a chain beats a stack of separate ifs, which would keep checking and can fire more than once.

3

The Idea

elif is short for “else if.” Python checks each condition top to bottom and runs the block for the first one that is True — then skips the entire rest of the chain. An optional else at the end catches everything that matched nothing.

The bug this prevents. If you write the grade checker as four separate if statements instead of a chain, a score of 95 is >= 90 and >= 80 and >= 70… so it can print several grades. An elif chain stops at the first match, so it prints exactly one. Order matters: highest threshold first.

Python — grade classifier

score = 85
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
else:
    print("D or F")

AP Pseudocode

score ← 85
IF (score ≥ 90)
{ DISPLAY("A") }
ELSE IF (score ≥ 80)
{ DISPLAY("B") }
ELSE IF (score ≥ 70)
{ DISPLAY("C") }
ELSE
{ DISPLAY("D or F") }

Hover or tap a line to light its twin. Trace score = 85: 85 >= 90? No. 85 >= 80? Yes — print B and stop. The >= 70 check never even runs.

Nesting is a conditional inside another conditional’s block. You only ask the inner question if the outer one already passed — like “is it raining? if so, do I have an umbrella?” The umbrella question is meaningless unless it’s raining.

Python — nested

n = 7
if n > 0:
    if n % 2 == 0:
        print("positive even")
    else:
        print("positive odd")
else:
    print("not positive")

What the indentation means

# outer question: is n positive?
# if yes, ask the inner question
#   inner: is it even?
#   deepest indent = both True
#
#   positive but odd
# outer else: n was 0 or negative
#   inner question never asked

For n = 7: positive? yes → even? no → prints positive odd. The indentation is the whole story — count the levels.

Reading deeply nested pseudocode is a top-three hardest exam skill. The trick is mechanical, not clever: track one variable in a table, and for each condition write True or False and follow only the matching braces. Never try to hold it all in your head — the questions are designed to punish exactly that.
4

Try It — The Grade Classifier

Enter a score and watch the elif chain check conditions top to bottom, stopping at the first match. The greyed-out rows are the checks that never run.

score =
Presets:
5

Vocabulary

elif chain Programs
A sequence of conditions checked in order; the block for the first True condition runs and the rest are skipped. Sorts a value into many mutually exclusive buckets.
nested conditional Programs
A conditional placed inside another conditional’s block. The inner condition is only tested when the outer one is True.
6

Check

Five traces. Use a trace table — one variable, each condition marked True or False, follow only the matching block. Then pick your answer.

0 of 5 answered
7

Impact Check

Order of checks = who gets what

An elif chain doesn’t just compute an answer — the order of its conditions decides outcomes. A benefits program that checks “income under $20k?” before “income under $40k?” hands out different aid than one that checks them in the other order. Same conditions, different order, different lives affected.

This is a quiet but real source of software bugs and unfairness: reorder the branches and you change who falls into which bucket. When you write a chain — or read someone else’s — the order is a design decision worth questioning, not just a formatting detail.

8

Connections

Came from 2.12 — a single if/else had two paths; elif stretches that to as many as you need.
Next in 2.14 — instead of many separate checks, you’ll combine conditions into one with and, or, and not.
Returns in 2.15 — the ticket-price calculator is an elif chain over age brackets, exactly like today’s grade classifier.
Returns on the exam — deeply nested conditionals in pseudocode are among the hardest and most reliable question types. The trace-table habit is your whole defense.
← 2.12 Selection: if / else