All subjects · Computer Science · WCP02 Practical Programming and Problem-solving
Selection: if, elif, else and match
Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving
Selection lets a program choose which code to run. Python offers if/elif/else and the match statement.
if, elif and else (spec 7.1.1)
An if statement tests a condition. If it is True, the indented block runs. elif (else if) tests another condition only if the earlier ones were False. else runs if none were True. Only the first True branch runs.
mark = 68
if mark >= 70:
print("A")
elif mark >= 60:
print("B")
elif mark >= 50:
print("C")
else:
print("U")Output:
BOrder of conditions
Put the most restrictive test first. If mark >= 50 were tested first, a mark of 68 would print C and stop. Indentation (4 spaces) defines the block: a line that is not indented is outside the if.
Nested selection
An if can sit inside another if. Nesting is sometimes needed, but deep nesting is hard to read; the clean code lesson shows how to flatten it with Boolean operators and guard clauses.
match and case
match compares one value with several patterns. case _ (or a name such as case other) is the default when nothing else matches. The | symbol combines patterns.
day = "Sat"
match day:
case "Sat" | "Sun":
print("Weekend")
case "Fri":
print("Nearly")
case _:
print("Weekday")Output:
WeekendWorked example
Write selection that prints 'child' for age under 13, 'teen' for 13 to 19 and 'adult' otherwise. What is printed for age 13?
- if age < 13: print("child")
- elif age <= 19: print("teen")
- else: print("adult")
- For 13: age < 13 is False, age <= 19 is True, so: teen
Answer: teen
Check yourself
- What is displayed when this code runs?
```python
t = 25
if t > 30:
print("hot")
elif t > 20:
print("warm")
elif t > 10:
print("mild")
else:
print("cold")
```
Show answer
warm. 25 > 30 is False; 25 > 20 is True, so warm is printed and the remaining tests are skipped.
- What is displayed when this code runs?
```python
score = 95
if score > 50:
print("pass")
elif score > 90:
print("distinction")
```
- distinction
- pass / distinction
- pass
- Nothing is displayed
Show answer
pass. score > 50 is True first, so only pass is printed: the distinction test is never reached. The tests are in the wrong order.
- What is displayed when this code runs?
```python
x = 3
if x > 5:
print("big")
print("done")
```
Show answer
done. The if block is skipped because 3 > 5 is False, but print("done") is not indented, so it always runs: done.
- What is displayed when this code runs?
```python
code = 404
match code:
case 200:
print("OK")
case 404:
print("Not found")
case _:
print("Other")
```
Show answer
Not found. code matches case 404, so Not found is displayed.
- What is displayed when this code runs?
```python
colour = "teal"
match colour:
case "red" | "pink":
print("warm")
case "blue":
print("cool")
case _:
print("unknown")
```
Show answer
unknown. No pattern matches "teal", so the default case _ runs: unknown.
- This code should print 'even' or 'odd'. Which line completes it?
```python
n = 8
__________
print("even")
else:
print("odd")
```
- `if n % 2 == 1:`
- `if n / 2 == 0:`
- `if n // 2 == 0:`
- `if n % 2 == 0:`
Show answer
`if n % 2 == 0:`. n % 2 is the remainder after dividing by 2; it is 0 for even numbers. For n = 8 the remainder is 0, so 'even' is printed.
- This code should display adult but it does not. Which line number contains the error?
```
1 age = 18
2 if age > 18:
3 print("adult")
4 else:
5 print("minor")
```
Show answer
2. Line 2 uses > instead of >=, so an 18-year-old is wrongly classed as a minor. The fix is if age >= 18:. This is a boundary error.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification (XCP01/YCP01), first teaching 2026
- Pearson IAL Computer Science Unit 2 Programming Language Subset (PLS), Python 3
- Pearson IAL Computer Science IAS sample assessment materials (Unit 2 paper and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.