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.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

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:

B

Order 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:

Weekend

Worked example

Write selection that prints 'child' for age under 13, 'teen' for 13 to 19 and 'adult' otherwise. What is printed for age 13?

  1. if age < 13: print("child")
  2. elif age <= 19: print("teen")
  3. else: print("adult")
  4. For 13: age < 13 is False, age <= 19 is True, so: teen

Answer: teen

Key idea: Only the first True branch of if/elif/else runs, so order the tests carefully; match/case compares one value against patterns, with case _ as the default.

Check yourself

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

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

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

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

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

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

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

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.