Open the app
  1. Home
  2. Lessons
  3. GCSE & IGCSE Computer Science
  4. Selection: if, elif and else

Selection: if, elif and else

🎬 The doodle video for this lesson is coming soon. Subscribe on YouTube to see it first.

Selection lets a program make decisions. Python checks conditions from the top and runs the first block that is True.

if, elif, else

Python checks each condition in order and runs only the first block whose condition is True.
else runs if none of them is True.
The colon and the indentation show which lines belong to each block.
mark = 72
if mark >= 70:
    print("A")
elif mark >= 50:
    print("B")
else:
    print("C")

Combining conditions

Use and, or and not to build conditions.
if age >= 13 and age <= 19: is True only for teenagers.
if day == "Sat" or day == "Sun": is True at weekends.

Nested selection

An if inside another if is nested. The inner if is only checked when the outer condition is True.
Always test the boundaries: what happens at exactly 70 or 50?
Worked example

What does the grade program print when mark = 50?

  1. mark >= 70 is False.
  2. mark >= 50 is True, so print B and skip the rest.

Answer: B

Key idea

if, elif and else choose which block runs; only the first True branch runs. Conditions use == != < > <= >= with and, or, not. Indentation shows the blocks.

Check you have got it

Answer 6 quick questions with instant marking. If you get one wrong, GCSE-ready shows you why and gives you another go. It is free, and you do not need an account.