Programming Essentials~25 min

Conditions

Programs choose what to do next using Boolean expressions — and only one branch of a decision ever runs.

By the end of this lesson, you can

  • Write an if / elif / else chain that chooses between more than two outcomes
  • Trace which branch of a conditional runs for a given input
  • Explain why only one branch of an if / elif / else chain ever runs, and why separate if statements behave differently

Why it matters

So far, every program in this course has done the same thing no matter what values it started with. Real programs need to behave differently depending on circumstances: charge a different price for a child than an adult, print a different message for a passing grade than a failing one. A condition — a Boolean expression a program checks before deciding what to do — is how a program branches.

Mental model

Think of if as a fork in the road: the program evaluates a condition, and only one of the available paths is ever taken. Adding elif (“else if”) extends the fork to more than two paths, but the rule stays the same — as soon as one condition matches, its branch runs, and every later elif and the final else are skipped entirely.

A cinema charges different prices by age:

PythonA three-way decision
age = 10

if age < 12:
    price = 8
elif age >= 65:
    price = 10
else:
    price = 14

print(price)
Output
8

Python checks age < 12 first. It’s True, so price = 8 runs — and the elif and else are never even evaluated, regardless of what they say.

Trace it

Tracing a conditional means identifying which single branch runs for a given input:

Which branch runs for each age
ageage < 12age >= 65Branch takenprice
10True(not checked)if8
30FalseFalseelse14
70FalseTrueelif10

Notice the second row: for age = 70, Python only checks age >= 65 after confirming age < 12 is False. The condition on the elif line is never evaluated until every condition above it has failed.

Check your understanding

What does this print? score = 72 if score >= 90: grade = "A" elif score >= 70: grade = "B" elif score >= 50: grade = "C" else: grade = "F" print(grade)
Or reveal the answer without checking

Answer:B
72 fails score >= 90, so Python checks the next line: score >= 70 is True, so grade becomes 'B' and every condition after it is skipped — even though score >= 50 is also true.

An elif line's condition is checked...
Or reveal the answer without checking

Answer:Only if every condition above it was False
Python only evaluates an elif's condition once every earlier condition in the same chain has failed. As soon as one branch matches, the rest of the chain is skipped.

Practice: warm-up

For each age value below, write down which branch of the cinema pricing example runs and the resulting price: 5, 12, 40, 65.

Stuck? Reveal one hint at a time.

  1. Hint 1

    Check the conditions top to bottom, in order, and stop at the first one that is True.

  2. Hint 2

    age = 12 fails age < 12 — remember < is strict, it does not include 12 itself.

Reveal the trace

Try the problem yourself before reading this. There is often more than one reasonable approach — treat this as one worked example, not the only correct answer.

age = 5   -> age < 12 is True   -> if branch    -> price = 8
age = 12  -> age < 12 is False, age >= 65 is False -> else branch -> price = 14
age = 40  -> both False                              -> else branch -> price = 14
age = 65  -> age < 12 is False, age >= 65 is True -> elif branch -> price = 10

Practice: apply it

This program is meant to label a temperature as "freezing", "cold", or "mild", but it has the same bug described above:

temperature = 5

if temperature <= 0:
    label = "freezing"
if temperature <= 15:
    label = "cold"
else:
    label = "mild"

print(label)
What does this actually print for temperature = 5?
Or reveal the answer without checking

Answer:"cold"
temperature <= 0 is False, so the first if does nothing. The second, independent if checks temperature <= 15, which is True, so label becomes "cold". Its else never runs because the if it belongs to succeeded.

Modification challenge: rewrite it as a single if / elif / elif / else chain so that temperature = -5 correctly reports "freezing" instead of being overwritten by a later branch.

Summary

  • A condition is a Boolean expression an if checks before deciding whether its branch runs.
  • In an if / elif / else chain, exactly one branch runs — the first whose condition is True — and every branch after it is skipped without being evaluated.
  • A sequence of separate if statements is different: each one is checked independently, every time, so more than one can run.
  • Tracing a conditional means checking each condition in order and stopping at the first match.