🎯 Syllabus & Goals 3 min
Cambridge 8.1.4 · Selection Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Write
IF … THEN … ENDIFandIF … THEN … ELSE … ENDIFstatements in pseudocode and Python. - Write a
CASE OF … OTHERWISE … ENDCASEstatement and its Pythonif … elif … elseequivalent. - Choose between
IFandCASEfor a given problem, and test both paths of a decision.
Textbook: Chapter 8, §8.1.4(b) (pp. 310–312)
Recap / Warm-Up 5 min
Last lesson: statements run in sequence, top to bottom. Today the program learns to take a different route depending on the data.
Quick starter
In Cambridge pseudocode, what is the difference between Age ← 18 and Age = 18?
Reveal the answer
← assigns 18 to Age. = compares Age with 18 and gives TRUE or FALSE. (Python uses = to assign and == to compare.)
🧠 Key Concept 14 min
Selection picks out data or actions that meet a rule: the largest value, items over a certain price, or customers old enough for a product. Cambridge pseudocode has two selection statements: IF and CASE.
1 · IF — one or two paths
Single choice
IF Age > 17
THEN
OUTPUT "You are an adult"
ENDIFWith an alternative
IF Age > 17
THEN
OUTPUT "You are an adult"
ELSE
OUTPUT "You are a child"
ENDIFThe same in Python
if age > 17: print("You are an adult")
if age > 17: print("You are an adult") else: print("You are a child")
Python uses a colon and indentation instead of THEN and ENDIF.
TRUE or FALSE.2 · CASE — many paths from one value
When one variable can have several different values, each needing its own action, a CASEstatement is clearer than a long chain of IFs. OTHERWISE catches every value not listed.
Cambridge pseudocode
CASE OF Operator "+" : Answer ← Number1 + Number2 "-" : Answer ← Number1 - Number2 "*" : Answer ← Number1 * Number2 "/" : Answer ← Number1 / Number2 OTHERWISE : OUTPUT "Invalid operator" ENDCASE
The same in Python
if operator == "+": answer = number1 + number2 elif operator == "-": answer = number1 - number2 elif operator == "*": answer = number1 * number2 elif operator == "/": answer = number1 / number2 else: print("Invalid operator")
OTHERWISE is the safety net.IF Weight > MaxLoad before closing its doors — a two-way decision. A phone's automated helpline ("press 1 for sales, 2 for support…") is a CASE: one key press, many possible routes, and "sorry, that is not an option" for anything else.Worked Example 12 min
Example 1 · IF … ELSE — a ride height check
A theme-park ride needs riders to be at least 120 cm tall. Input a height and output whether the person may ride.
- Store the limit as a constant.if the rule changes, only one line changes.
- Choose the operator carefully: "at least 120" means
>= 120.using>would wrongly refuse someone exactly 120 cm tall. - Test both paths and the boundary: 125, 110 and 120.each branch must be seen to work.
Cambridge pseudocode
CONSTANT MinHeight ← 120
DECLARE Height : INTEGER
OUTPUT "Enter your height in cm: "
INPUT Height
IF Height >= MinHeight
THEN
OUTPUT "You may ride"
ELSE
OUTPUT "Sorry, you are too short for this ride"
ENDIFThe same in Python
# Theme park ride: height check MIN_HEIGHT = 120 height = int(input("Enter your height in cm: ")) if height >= MIN_HEIGHT: print("You may ride") else: print("Sorry, you are too short for this ride")
Output — three test runs
Enter your height in cm: 125 You may ride Enter your height in cm: 110 Sorry, you are too short for this ride Enter your height in cm: 120 You may ride
| Height | Height >= 120? | Path taken | OUTPUT |
|---|---|---|---|
| 125 | TRUE | THEN | You may ride |
| 110 | FALSE | ELSE | Sorry, you are too short for this ride |
| 120 | TRUE | THEN | You may ride |
Example 2 · CASE — delivery cost by zone
A shop charges delivery by zone: A = $5.00, B = $7.50, C = $12.00. Any other zone is rejected.
- One variable (
Zone) decides between several fixed values.that is the signal to use CASE. - Each label is a CHAR, so it is written in single quotes.the label must match the variable's data type.
OTHERWISEsetsCost ← 0as a flag, and anIFafterwards chooses the message.every possible input now has a defined result.
Cambridge pseudocode
DECLARE Zone : CHAR
DECLARE Cost : REAL
OUTPUT "Enter delivery zone (A, B or C): "
INPUT Zone
CASE OF Zone
'A' : Cost ← 5.00
'B' : Cost ← 7.50
'C' : Cost ← 12.00
OTHERWISE : Cost ← 0
ENDCASE
IF Cost > 0
THEN
OUTPUT "Delivery cost: $", Cost
ELSE
OUTPUT "Unknown zone"
ENDIFThe same in Python
# Delivery cost by zone: Python has no CASE, so use if...elif...else zone = input("Enter delivery zone (A, B or C): ") if zone == "A": cost = 5.00 elif zone == "B": cost = 7.50 elif zone == "C": cost = 12.00 else: cost = 0 if cost > 0: print("Delivery cost: $", cost) else: print("Unknown zone")
Output — two test runs
Enter delivery zone (A, B or C): B Delivery cost: $ 7.5 Enter delivery zone (A, B or C): D Unknown zone
Try It Yourself 12 min
Goal: Input a number and output "100 or more" or "less than 100". Write it in pseudocode and Python, and test with 99, 100 and 101.
Goal: Input a number from 1 to 4 and use CASE to output "one", "two", "three" or "four". Any other value outputs "Out of range". Give the Python elif version too.
Goal: Input a test mark out of 100 and output a grade: A for 80+, B for 60–79, C for 40–59, otherwise U.
Hint
Pseudocode has no ELSEIF, so put a second IF inside the ELSE of the first (you will study this nesting in Lesson 7). Test the highest mark first — then Mark >= 60 already means "under 80". In Python, use elif.
📝 Exam Practice 10 min
Identify two different selection statements that can be used in pseudocode.
Mark scheme
IF … THEN … ELSE … ENDIF(1)CASE OF … OTHERWISE … ENDCASE(1)
Write pseudocode to input a temperature. If it is below 0, output "Freezing"; otherwise output "Above freezing".
Mark scheme
- Prompt and
INPUT Temperature(1). IF Temperature < 0— correct condition (1).THEN OUTPUT "Freezing"(1).ELSE OUTPUT "Above freezing"andENDIF(1).
Explain why a CASE statement may be more suitable than IF statements for a menu with six options.
Mark scheme
- One variable is compared with several different values (1)…
- …so the code is shorter / easier to read and maintain than many (nested) IF statements (1).
- OR: OTHERWISE handles every invalid choice in one place (1).
A program outputs a day's name from a number. Complete the pseudocode so that 1 outputs "Monday", 2 outputs "Tuesday", and any other number outputs "Invalid".
INPUT DayNumber CASE OF .......... .......... : OUTPUT "Monday" 2 : OUTPUT "Tuesday" .......... : OUTPUT "Invalid" ENDCASE
Mark scheme
CASE OF DayNumber(1)1 : OUTPUT "Monday"(1)OTHERWISE : OUTPUT "Invalid"(1)
🗝️ Recap & Key Terms 3 min
Selection chooses a path. Use IF for a condition with one or two outcomes, and CASE when one variable has several possible values. Python writes both with if, elif and else. Always test every path and the boundary.
- Selection
- Allowing the selection of different paths through the steps of a program.
- Condition
- An expression that evaluates to TRUE or FALSE, used to decide which path to take.
- IF statement
- Selection that runs one block when a condition is TRUE and, optionally, another block (ELSE) when it is FALSE.
- CASE statement
- Selection that compares one variable with a list of values and runs the matching block; OTHERWISE handles any other value.
Homework 1 min
Task (≤ 15 min): A cinema charges $6 for under-12s, $10 for ages 12–64 and $7 for 65 and over. Write pseudocode to input an age and output the ticket price. [5]
Model answer
DECLARE Age : INTEGER
DECLARE Price : REAL
OUTPUT "Enter your age: "
INPUT Age
IF Age < 12
THEN
Price ← 6
ELSE
IF Age < 65
THEN
Price ← 10
ELSE
Price ← 7
ENDIF
ENDIF
OUTPUT "Your ticket costs $", Price- Prompt and input age (1); under-12 test (1); 12–64 test inside the ELSE (1); 65+ as the final ELSE (1); output with message (1).