You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Selection allows a program to make decisions and follow different paths based on conditions. This is one of the three fundamental programming constructs (alongside sequence and iteration) and is required for AQA 3.2 / OCR J277 2.2.
Without selection, programs can only execute instructions in a fixed order (sequence). Selection lets programs respond to different inputs and situations, making them useful in the real world. For example, a login system must check whether a password is correct before granting access.
The simplest form of selection is the IF statement, which executes a block of code only if a condition is True.
IF age >= 18 THEN
OUTPUT "You can vote."
ENDIF
if age >= 18:
print("You can vote.")
Key Point: In Python, the colon
:at the end of theifline and the indentation of the body are essential. In pseudocode, the block ends withENDIF.
An IF...ELSE provides two paths — one for when the condition is True and one for when it is False.
IF score >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
if score >= 50:
print("Pass")
else:
print("Fail")
When there are more than two possible outcomes, use ELIF (short for "else if") to test additional conditions in sequence.
IF score >= 90 THEN
grade ← "A*"
ELSE IF score >= 80 THEN
grade ← "A"
ELSE IF score >= 70 THEN
grade ← "B"
ELSE IF score >= 60 THEN
grade ← "C"
ELSE
grade ← "U"
ENDIF
if score >= 90:
grade = "A*"
elif score >= 80:
grade = "A"
elif score >= 70:
grade = "B"
elif score >= 60:
grade = "C"
else:
grade = "U"
Exam Tip: The order of conditions matters. Place the most restrictive condition first. If you tested
score >= 60beforescore >= 90, a score of 95 would be graded as "C".
You can place one IF statement inside another. This is called nesting.
if age >= 18:
if has_id:
print("Entry allowed")
else:
print("ID required")
else:
print("Too young")
Nested selection can always be rewritten using logical operators (AND, OR, NOT):
if age >= 18 and has_id:
print("Entry allowed")
elif age >= 18:
print("ID required")
else:
print("Too young")
flowchart TD
A[Start] --> B{score >= 90?}
B -->|Yes| C[grade = A*]
B -->|No| D{score >= 80?}
D -->|Yes| E[grade = A]
D -->|No| F{score >= 70?}
F -->|Yes| G[grade = B]
F -->|No| H{score >= 60?}
H -->|Yes| I[grade = C]
H -->|No| J[grade = U]
C --> K[End]
E --> K
G --> K
I --> K
J --> K
| Operator | Meaning | Example |
|---|---|---|
AND | Both conditions must be True | age >= 18 AND has_ticket == True |
OR | At least one condition must be True | day == "Saturday" OR day == "Sunday" |
NOT | Reverses the Boolean value | NOT is_raining |
In Python these are written in lowercase: and, or, not.
A SWITCH (or CASE) statement selects one of many blocks of code based on the value of a single variable. It is an alternative to long chains of IF...ELIF.
SWITCH day:
CASE "Monday":
OUTPUT "Start of the week"
CASE "Friday":
OUTPUT "Nearly the weekend"
CASE "Saturday":
OUTPUT "Weekend!"
CASE "Sunday":
OUTPUT "Weekend!"
DEFAULT:
OUTPUT "Midweek"
ENDSWITCH
Python 3.10+ introduced match...case:
match day:
case "Monday":
print("Start of the week")
case "Friday":
print("Nearly the weekend")
case "Saturday" | "Sunday":
print("Weekend!")
case _:
print("Midweek")
Exam Tip: You may be asked to convert between IF...ELIF chains and SWITCH/CASE statements. Make sure you can do both fluently.
| Feature | IF...ELIF...ELSE | SWITCH/CASE |
|---|---|---|
| Tests | Any condition | A single variable against fixed values |
| Range checks | Yes (score >= 50) | No (exact matches only) |
| Flexibility | More flexible | Cleaner for many exact-match options |
| Readability | Can become long | Easier to read for menus |
elif conditions don't accidentally include values already covered.IF...THEN...ENDIF for a single condition.IF...THEN...ELSE...ENDIF for two paths.IF...ELIF...ELSE for multiple paths.SWITCH/CASE for matching a variable against many fixed values.AND, OR, NOT) combine conditions.A cinema charges different prices depending on the customer's age. Children under 5 are free, under 16 pay GBP 5, adults pay GBP 10, and over-65s pay GBP 7.
age = int(input("Customer age: "))
if age < 5:
price = 0
elif age < 16:
price = 5
elif age < 65:
price = 10
else:
price = 7
print(f"Ticket price: GBP {price}")
Trace for input 14:
| Condition | Evaluates to | Action |
|---|---|---|
age < 5 | False | skip |
age < 16 | True | price = 5, exit chain |
age < 65 | skipped | — |
| else | skipped | — |
Output: Ticket price: GBP 5
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.