N3/N4  ·  SDD  ·  Software Design & Development

Selection: if / else

National 3 & National 4 Computing Science Lesson SDD3 of 10 Approx 55 min
Learning intentions
  • Understand how selection lets a program make decisions
  • Use comparison operators to build a condition
  • Write if, if/else, and if/elif/else statements
Success criteria
  • I can write an if statement that checks a condition
  • I can add an else branch for when the condition is false
  • I can chain multiple conditions using elif
Warm up — what do you already know?

Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.

1. "If it is raining, take an umbrella." If it is NOT raining, what happens?

2. Which symbol usually means "equal to" in maths?

3. True or false: a program can only ever do the exact same thing every time it runs, with no decisions.

Key vocabulary

Selection
A programming construct that lets a program choose which instructions to run, based on a condition.
Condition
A statement that is either True or False, used to decide which path a program takes.
Comparison operator
A symbol used to compare two values, such as ==, <, or >=.
if
Runs a block of code only if its condition is True.
else
Runs a block of code only if the matching if condition was False.
elif
Short for "else if" — checks another condition if the previous one was False.

Selection: if / else

What is selection?

Selection is one of the three basic building blocks of any program, alongside sequence (running instructions in order) and iteration (repeating instructions). Selection lets a program choose between different paths depending on whether a condition is true or false. Without selection, every program would do exactly the same thing every single time it ran, regardless of any input.

Comparison operators

A condition is built using a comparison operator, which compares two values and produces a boolean result (True or False):

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
<Less than3 < 5True
>Greater than3 > 5False
<=Less than or equal to5 <= 5True
>=Greater than or equal to4 >= 5False

Notice that == (two equals signs) checks whether two values are equal, while a single = is used for assignment — storing a value in a variable. These do two completely different jobs, and mixing them up is one of the most common beginner errors.

The if statement

An if statement runs a block of code only when its condition is True. In Python, the structure is: if condition: followed by an indented block underneath. The colon and the indentation are both required — Python uses indentation (not brackets, unlike many other languages) to show which lines belong inside the if.

if / else and if / elif / else

An else branch runs when the if condition was False, covering "everything else." When there are more than two possible paths, elif ("else if") checks an additional condition, and you can chain as many elif branches as needed before an optional final else. Python checks each condition in order from top to bottom and runs only the first block whose condition is True — it does not check any further conditions after that.

Worked examples

Example 1 — a simple if statement

A program checks whether someone is old enough to vote.

1
age = 16
2
if age >= 18: — the condition is checked.
3
    print("You can vote.") — this indented line only runs if the condition above was True. Since age is 16, nothing is printed here.
Example 2 — if / else

A program checks whether a test score is a pass or a fail.

1
score = 42
2
if score >= 50:
    print("Pass")
3
else:
    print("Fail")
Since 42 is less than 50, this program outputs Fail.
Example 3 — if / elif / else for grade bands

A program assigns a grade based on a percentage score.

1
score = 72
2
if score >= 90: print("A")
elif score >= 70: print("B")
elif score >= 50: print("C")
else: print("Fail")
3
Python checks each condition in order. 72 is not ≥ 90, but it is ≥ 70, so B is printed and the remaining conditions are never checked.
Now you try

A program offers a discount to customers: if age < 12: print("Child discount"), elif age >= 65: print("Senior discount"), else: print("Full price").

Answer the following:

  1. What is printed if age is 8?
  2. What is printed if age is 70?
  3. What is printed if age is 30?
  1. Child discount — 8 is less than 12, so the first condition is True.
  2. Senior discount — 70 is not less than 12, but it is greater than or equal to 65.
  3. Full price — 30 fails both the if and elif conditions, so the else branch runs.
Common mistakes
Using = instead of ==. if age = 18: is invalid — a single = is for assignment, not comparison. Python will raise a SyntaxError.
Missing or inconsistent indentation. Python uses indentation to know which lines belong inside the if. Missing it, or mixing tabs and spaces, causes an IndentationError.
Forgetting the colon :. Every if, elif, and else line must end with a colon before the indented block.
Writing separate if statements instead of elif. Using several independent if statements means Python checks every single one, even after an earlier condition matched — this can cause more than one message to print when only one was intended.
Tip

N4 pupils: when explaining selection in your evidence, describe the flow — which condition is checked first, what happens if it's true, and what happens if it's false — rather than just describing what each line does in isolation.

Task Set

Questions 1–5 are auto-checked. Questions 6–7 are self-marked — write your answer, then reveal the model answer to check your work. Questions 8–9 are practical tasks in IDLE.

1. Which symbol checks if two values are equal in Python? TYPE 1

2. Given score = 45, what does if score >= 50: print("Pass") else: print("Fail") output? TYPE 1

3. What causes an IndentationError in Python? TYPE 1

4. In an if / elif / else chain, what happens once Python finds a condition that is True? TYPE 1

5. Given temp = 30, which branch runs in if temp > 25: print("Hot") elif temp > 15: print("Mild") else: print("Cold")? TYPE 1

6. Explain the difference between = and == in Python, using an example of each. TYPE 2 N4

A single = is the assignment operator — it stores a value in a variable, for example age = 13 stores 13 in age. A double == is the equality comparison operator — it checks whether two values are equal and produces a boolean result, for example age == 13 checks whether age currently holds the value 13, without changing it.

7. Explain why writing three separate if statements instead of if / elif / else can cause a program to behave unexpectedly. TYPE 2 N3

Separate if statements are each checked independently, so Python tests every single one even after an earlier condition has already matched. If more than one condition happens to be true at once, this could cause more than one message to print, when the programmer only intended one branch to run. Using elif guarantees only one branch in the chain ever runs.

8. Practical: In IDLE, create a new file called sdd3_selection.py. Write a program that asks the user for a test score (as an integer) and prints "Pass" if it is 50 or above, or "Fail" otherwise. Test it with 65 and with 30. Record your output below. TYPE 3

score = int(input("Enter your score: "))
if score >= 50:
    print("Pass")
else:
    print("Fail")


Expected output: Pass for 65, Fail for 30.

9. Practical: In sdd3_selection.py, extend your program to give a grade instead: "A" for 90+, "B" for 70+, "C" for 50+, or "Fail" otherwise, using elif. Test with 95, 75, 55, and 20. Record your output. TYPE 3

if score >= 90:
    print("A")
elif score >= 70:
    print("B")
elif score >= 50:
    print("C")
else:
    print("Fail")


Expected output: A, B, C, Fail respectively.
Teacher notes — Shift+T to hide

Suggested timing: 55 minutes. Warm up 5 min; notes 15 min; examples 10 min; now you try 5 min; task set 20 min.

Key misconception to address: = vs == confusion is near-universal at this stage — drill it explicitly with side-by-side examples.

Live demo suggestion: live-code the grade-bands example, then change the score value and re-run repeatedly so pupils see the flow change.

Extension idea (S5 / N5-bridging, non-assessed): briefly show combining conditions with and/or as a "look ahead" — keep clearly separate from the assessed task set.

Assessment standards covered: N3 O2.1; N4 O1.2, O2.1.