If, Else and Match
Level 3 · Decisions and loops
How a program makes choices with if, elif and else. How control flow skips lines, and how match checks one value against many cases.
Course contentsLesson 10 of 18
Level 1 · What a program is
Level 2 · Values and variables
Level 3 · Decisions and loops
Level 4 · Functions
Level 5 · Errors and debugging
Level 6 · Good code habits
After this lesson you can
- Write an if, elif and else chain
- Explain what control flow means
- Use match to check one value against cases
12 mincovers 3 glossary entries
Worth reading first: Operators in Python, Code Blocks and Indentation
How a program decides which lines to run
Control flow is the order in which a program runs its lines. Without it, every line runs once, top to bottom. With it, a program can skip lines, choose between them, or repeat them. A conditional statement is the tool for choosing: an if runs its block only when a test is True.
In Python, the line starts with if, then a test, then a colon. The block under it is pushed in by 4 spaces. if temp > 30: followed by print("Hot") prints Hot only on hot days. On other days Python skips the block and carries on below it.
else gives the other path. Its block runs when the test is False, so one of the two always runs. elif, short for else if, adds more tests in between. Python checks them from the top and runs the first block whose test is True. Then it skips the rest of the chain.
That order matters. Test score >= 50 before score >= 90, and a score of 95 stops at the first test and never reaches the second. So put the narrowest test first. Read each chain from the top, the way Python does, and ask which line a given value stops at.
When one value is checked against many fixed choices, a long elif chain gets hard to read. Many languages have a switch statement for this. Python's version, added in 3.10 in 2021, is called match. You write match day:, then one case line per value, and case _: catches anything left over.
match suits a menu choice, a command word or a status number. if suits ranges and mixed tests, such as age over 18 and has a ticket. Both do the same basic job. Pick the one that makes the choice easiest to read.
A common slip is to write a test that can never be True, such as age < 13 and age > 19. No age is both. The block never runs, and nothing warns you. When a branch never seems to fire, read its test aloud.
score = 72
if score >= 90:
grade = "A"
elif score >= 70:
grade = "B"
else:
grade = "C"
print(grade) # B
match day:
case "Sat" | "Sun":
print("Weekend")
case _:
print("Weekday")Worked example
Jack's grading script gives every top pupil a C
Jack's chain tested score >= 50 first, then score >= 90. All 7 pupils who scored over 90 got the grade meant for 50.
Python stopped at the first True test. He moves the 90 test to the top. Now the 7 top pupils get an A.
The words for this lesson3
Check yourself
In an if, elif and else chain, which blocks run?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.