Spec & Goals 3 min
AQA Spec 3.2.2 · Programming constructs — selection
By the end of this lesson you can:
- Explain what selection is and identify the condition that controls it.
- Write an IF … THEN … ELSE … ENDIF statement, including multi-way selection with
ELSE IF. - Trace a program to state which branch runs for a given input.
Warm-Up 5 min
Last lesson you stored values in variables and constants. Now we make programs decide what to do with them.
Quick starter
You buy nasi lemak for RM 5 and pay with RM 10. A till must decide whether to give change. Which of these is the condition the till tests?
- Print the receipt.
- Is the amount paid greater than the price?
- Add the items together.
Reveal the answer
"Is the amount paid greater than the price?" is the condition. It is a question with a true/false answer, and it decides which path the program takes.
Key Concept — making decisions with selection 14 min
Selection chooses which block of code runs, based on whether a condition is true or false. It is one of the three basic programming constructs.
The basic IF statement
An IF statement runs one block when the condition is true, and an optional ELSE block when it is false.
AQA pseudo-code
IF age ≥ 18 THEN OUTPUT 'Adult' ELSE OUTPUT 'Minor' ENDIF
Multi-way selection with ELSE IF
When there are more than two paths, add ELSE IF branches. The program tests each condition in order and runs the first one that is true.
IF speed > 110 THEN OUTPUT 'Too fast' ELSE IF speed > 90 THEN OUTPUT 'Watch your speed' ELSE OUTPUT 'Speed OK' ENDIF
Nested IF
A nested IF is an IF statement placed inside another. The inner test only happens when the outer condition is true.
IF loggedIn = True THEN
IF isAdmin = True THEN
OUTPUT 'Admin menu'
ELSE
OUTPUT 'User menu'
ENDIF
ENDIFWorked Example — a grade classifier 12 min
Problem: input an exam mark out of 100 and output the grade: A for 70+, B for 60–69, C for 50–59, otherwise U.
AQA pseudo-code
mark ← USERINPUT IF mark ≥ 70 THEN OUTPUT 'A' ELSE IF mark ≥ 60 THEN OUTPUT 'B' ELSE IF mark ≥ 50 THEN OUTPUT 'C' ELSE OUTPUT 'U' ENDIF
The same algorithm in Python
mark = int(input("Enter the mark: ")) if mark >= 70: print("A") elif mark >= 60: print("B") elif mark >= 50: print("C") else: print("U")
Trace which branch runs when the user enters mark = 64:
| Condition tested | Result | Branch taken? | Output |
|---|---|---|---|
mark ≥ 70 | False | No | — |
mark ≥ 60 | True | Yes | B |
mark ≥ 50 | not tested | skipped | — |
The output is B. Once mark ≥ 60 is true, the remaining branches are skipped.
Try It Yourself 12 min
Goal: Write an IF … THEN … ELSE … ENDIF that inputs a temperature and outputs 'Hot' if it is above 30, otherwise 'Comfortable'.
Hint: the condition is temperature > 30.
Goal: Write multi-way selection that inputs a score and outputs 'Gold' for 90+, 'Silver' for 75+, otherwise 'Bronze'.
Hint: use ELSE IF and put the highest threshold first.
Goal: Write a nested IF: only if a user is member = True, check whether their points ≥ 100 and output 'Free gift' or 'Keep collecting'.
📝 Exam Practice 10 min
Answer the way the examiner expects — the command word and the marks tell you how much to write.
Describe what is meant by selection in a program.
Mark scheme
- A construct that chooses which code / which branch to run (1).
- Based on whether a condition is true or false (1).
A shop gives a 10% discount when total ≥ 100. Complete the IF statement so it outputs the amount to pay.
total ← USERINPUT IF __________ THEN OUTPUT total * 0.9 __________ OUTPUT __________ ENDIF
Mark scheme
- Condition
total ≥ 100(1). ELSEon the second blank (1).OUTPUT totalon the final blank (1).
Look again at the grade-classifier pseudo-code. State the output when the user enters mark = 50.
Mark scheme
C(1) —mark ≥ 70andmark ≥ 60are false,mark ≥ 50is true.
State the term for an IF statement placed inside another IF statement.
Mark scheme
- Nested IF (1).
Recap & Key Terms 3 min
Selection lets a program decide what to do. An IF statement tests a condition and runs one branch. ELSE IF gives multi-way choices, and a nested IF puts one decision inside another.
- Selection
- A construct that uses a condition to choose which code to run.
- Condition
- An expression that is either true or false, used to control selection.
- IF statement
- The structure
IF … THEN … ELSE … ENDIFthat carries out selection. - Nested IF
- An IF statement contained inside another IF statement.
Homework 1 min
Task (≤ 15 min): Write AQA pseudo-code that inputs a year and outputs whether it is a leap year (divisible by 4) using selection.
Model answer
year ← USERINPUT IF year MOD 4 = 0 THEN OUTPUT 'Leap year' ELSE OUTPUT 'Not a leap year' ENDIF
Award marks for: input the year (1), correct condition using MOD (1), correct output on each branch (1).