# If, Else and Match

> 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:** [Programming Basics](https://learn.benibix.com/learn/development-concepts)  
**Time:** 12 min

**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

## 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.

```python
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")
```
Python runs the first branch whose test is True. In match, case _ catches every value not listed.

![A value, day equals Sun, is checked against a column of cases in turn: Sat, then Sun, then Mon, then a default. The Sun case matches and prints Weekend. The default sits at the bottom and runs only when nothing matches.](https://learn.benibix.com/diagrams/dev7-switch-statement.svg)

### 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.

**Words in this lesson:** [Control Flow](https://learn.benibix.com/term/development/control-flow), [Conditional Statement](https://learn.benibix.com/term/development/conditional-statement), [Switch Statement](https://learn.benibix.com/term/development/switch-statement)

**Question:** In an if, elif and else chain, which blocks run?

**Answer:** Only the first block whose test is True. Python checks the tests from the top and runs the first block whose test is True. It skips the rest of the chain, even if later tests are True too.

---

Source: https://learn.benibix.com/learn/development-concepts/if-else-and-match
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
