🎯 Syllabus & Goals 3 min
Cambridge 7.2 · Design methods — pseudocode Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Write assignment statements with
←and work out the values they produce. - Write
IF … THEN … ELSE … ENDIF, including a nested IF, using comparison and Boolean operators. - Write a
CASE OF … OTHERWISE … ENDCASEstatement and say when it is the better choice.
Textbook: Chapter 7, §7.2.3 Pseudocode — assignment and conditional statements (pp. 265–268)
Recap / Warm-Up 5 min
Last lesson we drew algorithms as flowcharts. Pseudocode shows the same algorithm as text. Every rectangle becomes an assignment; every decision becomes an IF or a loop.
Quick starter
In the pool flowchart, which symbol became IF N < 5 in the pseudocode, and which became D ← 0?
Reveal the answer
The decision diamond became the IF; the process rectangle became the assignment D ← 0.
🧠 Key Concept 14 min
1 · What pseudocode is
Pseudocode describes an algorithm with English keywords that look like a high-level language. Data items get meaningful names, like variables. But it has no strict syntax rules — it pretends to be code. Cambridge writes it consistently:
- a fixed-width (non-proportional) font;
- keywords in CAPITALS:
INPUT,IF,ENDIF; - names of data items start with a capital letter:
StudentMark,Total; - statements inside an
IFor a loop are indented by two spaces; - on this site, always one statement per line.
2 · Assignment
A value is given to a variable with ←. The variable on the left takes the value of the expression on the right.
Price ← Cost * 2 works right to left: read Cost, work out the expression, then store the answer in Price. Cost itself does not change.| Operator | Action | Example | Result |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
/ | divide | 7 / 2 | 3.5 |
^ | raise to the power | 2 ^ 3 | 8 |
( ) | group — worked out first | (7 + 2) * 2 | 18 |
3 · Selection with IF … THEN … ELSE … ENDIF
When an algorithm must choose between actions, it uses a conditional statement. The THEN path runs if the condition is true; the ELSE path runs if it is false. There may be no ELSE. ENDIF always closes the statement.
Condition is a comparison
IF Speed > 50
THEN
OUTPUT "Slow down"
ELSE
OUTPUT "OK"
ENDIFCondition is a Boolean variable
IF Paid
THEN
OUTPUT "Ticket printed"
ELSE
OUTPUT "Please pay first"
ENDIF| Operator | Meaning | Operator | Meaning |
|---|---|---|---|
> | greater than | <= | less than or equal to |
< | less than | <> | not equal to |
= | equal to | AND | both must be true |
>= | greater than or equal to | OR / NOT | either is true / reverses true and false |
4 · Selection with CASE OF … OTHERWISE … ENDCASE
A CASE statement chooses a path from the value of one variable. Several values are listed. OTHERWISE catches every other value. ENDCASE closes it.
CASE: one action per button number. A barrier that opens only for a valid ticket is an IF: a single true/false test.Worked Example 12 min
Worked example 1 · Values after a sequence of assignments
A decorator works out how much paint a wall needs. What does each variable hold at the end?
Width ← 4 Height ← 2.5 Area ← Width * Height Coats ← 2 Paint ← Area * Coats / 5 Label ← "Litres: "
- Width ← 4; Height ← 2.5.
- Area ← 4 × 2.5 = 10.the right side uses the values Width and Height hold at that moment.
- Coats ← 2.
- Paint ← 10 × 2 / 5 = 20 / 5 = 4.
*and/have equal priority, so work left to right. - Label ← Litres: (a string, stored without the quotation marks).
| Width | Height | Area | Coats | Paint | Label |
|---|---|---|---|---|---|
| 4 | 2.5 | 10 | 2 | 4 | Litres: |
width = 4 height = 2.5 area = width * height coats = 2 paint = area * coats / 5 label = "Litres: " print(label, paint)
Litres: 4.0
Worked example 2 · A nested IF for a quiz score
A quiz is marked out of 50. Scores below 0 or above 50 are invalid. A score of 25 or more is a pass.
OUTPUT "Enter the score "
INPUT Score
IF Score < 0 OR Score > 50
THEN
OUTPUT "Invalid score"
ELSE
IF Score >= 25
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDIFscore = int(input("Enter the score ")) if score < 0 or score > 50: print("Invalid score") else: if score >= 25: print("Pass") else: print("Fail")
- Outer IF checks validity first.
ORis used because a score is invalid if either test is true.a score cannot be below 0 and above 50 at once, so AND would never be true. - Inner IF runs only for valid scores. It sits in the outer
ELSE, indented one more level, with its ownENDIF. - Test it:
25 and 24 sit either side of the pass mark — the most likely place for a mistake.Score Outer test Inner test Output 60 60 > 50 → true not reached Invalid score 25 false 25 >= 25 → true Pass 24 false 24 >= 25 → false Fail
Worked example 3 · A CASE statement for a drinks machine
Button 1 is water ($1.00), 2 is juice ($1.50), 3 is cola ($1.20). Any other button is an error.
INPUT Button
Price ← 0
CASE OF Button
1 : Price ← 1.00
2 : Price ← 1.50
3 : Price ← 1.20
OTHERWISE OUTPUT "No such drink"
ENDCASE
IF Price > 0
THEN
OUTPUT "Please pay $", Price
ENDIFbutton = int(input("Button: ")) price = 0 if button == 1: price = 1.00 elif button == 2: price = 1.50 elif button == 3: price = 1.20 else: print("No such drink") if price > 0: print("Please pay $", price)
Button: 2 Please pay $ 1.5
- Why CASE here? One variable, several fixed values.three nested IFs would do the same job but are harder to read.
- Why
Price ← 0first? So that afterOTHERWISEthe finalIFknows no drink was chosen. - Python writes the same choice as
if … elif … else;==is Python's "equal to", andelseplays the part ofOTHERWISE.
Try It Yourself 12 min
Goal: State the final value of each variable: Hours ← 3, Rate ← 12, Pay ← Hours * Rate, Bonus ← Pay / 4, Pay ← Pay + Bonus.
Goal: Rewrite the quiz algorithm for a test marked out of 20 with a pass mark of 10. Then write it in Python.
Goal: Use a CASE statement to output the name of a month when Month holds a whole number from 1 to 12, and an error message otherwise. Then explain why a CASE is better here than nested IF statements.
Hint
One line per value: 1 : OUTPUT "January" … then OTHERWISE OUTPUT "Invalid month" and ENDCASE. Count how many ENDIFs the IF version would need.
📝 Exam Practice 10 min
State the values of Cost, Tax and Total after these statements run:
Cost ← 40 Tax ← Cost * 0.2 Cost ← Cost - 5 Total ← Cost + Tax
Mark scheme
- Cost = 35 (1)
- Tax = 8 (worked out before Cost changed) (1)
- Total = 43 (1)
Write pseudocode that inputs a person's age and outputs "Child" if the age is under 13, "Teenager" if it is 13 to 19, and "Adult" otherwise.
Mark scheme
INPUT Age(1)- Correct first test, e.g.
IF Age < 13…OUTPUT "Child"(1) - Correct nested/second test, e.g.
IF Age <= 19…OUTPUT "Teenager"(1) ELSE OUTPUT "Adult"with everyIFclosed byENDIF(1)
This pseudocode should output "Discount" for orders of $100 or more. Identify three errors.
INPUT Order
IF Order > 100
THEN
Discount = TRUE
OUTPUT DiscountMark scheme
>should be>=(100 itself must qualify) (1).=used for assignment; should beDiscount ← TRUE(1).- Output should be the string
"Discount", not the variable (1). - Missing
ENDIF(1). Max 3.
Explain when a CASE statement is more suitable than an IF statement.
Mark scheme
- When one variable is tested against several possible values (1)…
- …it is clearer / shorter than many nested IFs, and OTHERWISE handles every other value (1).
🗝️ Recap & Key Terms 3 min
Pseudocode uses capital keywords, capitalised names and two-space indents. ← assigns the value on the right to the variable on the left. IF chooses between two paths; a nested IF adds more; CASE chooses one path per value of a variable.
- Pseudocode
- A simple way of showing an algorithm using English keywords like a high-level language, without strict syntax rules.
- Assignment
- Giving a variable a value with
←; the variable on the left takes the value of the expression on the right. - Conditional statement (selection)
- A statement that chooses which action to take depending on a condition or a value.
- Nested IF
- An IF statement placed inside the THEN or ELSE path of another IF statement.
- CASE statement
- Selection where the value of one variable decides the path; OTHERWISE covers all other values.
Homework 1 min
Task (≤ 15 min): A parcel company charges $4 for parcels up to 2 kg, $7 for over 2 kg up to 10 kg, and $12 for over 10 kg up to 30 kg. Heavier parcels are refused. Write pseudocode that inputs a weight and outputs the charge or "Refused". [5]
Model answer
INPUT Weight
IF Weight > 30
THEN
OUTPUT "Refused"
ELSE
IF Weight > 10
THEN
OUTPUT "$12"
ELSE
IF Weight > 2
THEN
OUTPUT "$7"
ELSE
OUTPUT "$4"
ENDIF
ENDIF
ENDIFMarks: input (1); refuse over 30 (1); correct $12 band (1); correct $7 / $4 bands (1); every IF closed with ENDIF and correct indentation (1).