This page covers the CASE (also called switch) selection construct required by the Cambridge IGCSE/A‑Level Computer Science (9618) syllabus. It explains when to use CASE, the exact pseudocode syntax prescribed by the Cambridge Pseudocode Guide, how to build a CASE step‑by‑step, common pitfalls, and provides three worked examples plus practice tasks that address AO1, AO2 and AO3.
| Question | Recommended construct |
|---|---|
| Is the decision based on a single variable/expression? | YES → CASE NO → IF‑ELSE |
| Are there only two possible outcomes? | YES → IF … THEN … ELSE |
| Are there three or more distinct constant values? | YES → CASE |
| Do you need to test ranges (e.g. ≥ 90) or complex Boolean expressions? | YES → IF‑ELSE (or nested IF) |
value : line begins with a single space before the value and a single space before the colon.END CASE on its own line.← and the integer‑division operator DIV exactly as shown in the Cambridge Pseudocode Guide.| Component | Pseudocode |
|---|---|
| Start of CASE | CASE expression OF |
| First option | value1 : statements |
| Additional options | value2 : statements |
| Default option (optional) | OTHERWISE : statements |
| End of CASE | END CASE |
The expression and every value must be of the same data type (integer, character or string). The OTHERWISE clause is strongly recommended for a robust solution and to satisfy AO2/AO3 requirements.
CASE expression OF on a new line.OTHERWISE clause for any values not explicitly listed.END CASE.CASE month OF ‘Jan’:) is a syntax error and loses marks.CASE month OF // integer expression1 : days ← 31
2 : days ← 28
…
END CASE
CASE grade OF // character expression
'A' : comment ← "Excellent"
'B' : comment ← "Good"
OTHERWISE : comment ← "Other"
END CASE
When analysing a problem you should consider:
Therefore, in a design answer you should explicitly state that the problem meets the criteria in Section 1 and that CASE is the most appropriate construct.
END CASE – always close the block.>= 90) – CASE matches exact values only.OTHERWISE when an invalid input is possible – leads to undefined behaviour.DIV (or \ in some textbooks) to obtain the tens digit, not “÷ · 10”.We have a variable mark (0–100) and need to assign a letter grade. Because the required ranges are not single values, we first convert the mark to a “grade band” using integer division, then apply a CASE.
READ markSET band ← mark DIV 10 // integer division – tens digit
CASE band OF
10 : grade ← 'A' // mark = 100
9 : grade ← 'A' // 90–99
8 : grade ← 'B' // 80–89
7 : grade ← 'C' // 70–79
6 : grade ← 'D' // 60–69
OTHERWISE : grade ← 'F' // 0–59
END CASE
WRITE grade
DIV is usedDIV returns the integer quotient of the division, discarding any remainder. For example, 85 DIV 10 = 8, which maps directly to the CASE value for a ‘B’ grade. This follows the Cambridge Pseudocode Guide’s requirement for integer division.
This example shows a CASE nested inside a pre‑condition loop. The loop repeats the menu until the user selects “Exit”.
SET running ← TRUESET total ← 0
WHILE running DO
WRITE "Menu:"
WRITE "1 – Add a number"
WRITE "2 – Show total"
WRITE "3 – Exit"
READ choice
CASE choice OF
1 :
READ value
total ← total + value
2 :
WRITE "Current total = ", total
3 :
running ← FALSE
OTHERWISE :
WRITE "Invalid option – try again"
END CASE
END WHILE
WRITE "Program finished."
A two‑level menu: first choose a module, then choose an operation within that module.
READ module // 1 = Maths, 2 = ScienceCASE module OF
1 : // Maths module
READ operation // 1 = Add, 2 = Subtract
CASE operation OF
1 : result ← a + b
2 : result ← a - b
OTHERWISE : WRITE "Invalid maths operation"
END CASE
2 : // Science module
READ operation // 1 = Heat, 2 = Light
CASE operation OF
1 : WRITE "Heat experiment"
2 : WRITE "Light experiment"
OTHERWISE : WRITE "Invalid science operation"
END CASE
OTHERWISE : WRITE "Invalid module"
END CASE
This demonstrates how CASE structures can be layered – a skill required for the structure‑chart component of the syllabus (12.2).
All syntax follows the Cambridge Pseudocode Guide (see https://www.cambridgeinternational.org/support-and-training/pseudocode-guide). In particular:
←.DIV.Task: Write pseudocode using a CASE structure to determine the number of days in a month. The variable month holds an integer from 1 (January) to 12 (December). Assume February always has 28 days.
month.days.days.Suggested solution
READ monthCASE month OF
1 : days ← 31
2 : days ← 28
3 : days ← 31
4 : days ← 30
5 : days ← 31
6 : days ← 30
7 : days ← 31
8 : days ← 31
9 : days ← 30
10 : days ← 31
11 : days ← 30
12 : days ← 31
OTHERWISE : days ← -1 // indicates an invalid month
END CASE
WRITE days
Scenario: A bank wants a console‑based ATM program that repeatedly offers the following options:
Write pseudocode that uses a WHILE loop and a CASE statement to implement the menu. Then, in a short paragraph (2–3 sentences), justify why a CASE is the optimal selection construct for this problem.
Guidelines for the solution:
balance ← 0 and a Boolean running ← TRUE.option.option to perform the required action.OTHERWISE clause to handle invalid selections.Sample answer (students may vary):
SET balance ← 0SET running ← TRUE
WHILE running DO
WRITE "ATM Menu:"
WRITE "1 – Check balance"
WRITE "2 – Deposit"
WRITE "3 – Withdraw"
WRITE "4 – Exit"
READ option
CASE option OF
1 :
WRITE "Balance = $", balance
2 :
WRITE "Enter amount to deposit:"
READ amount
IF amount > 0 THEN
balance ← balance + amount
WRITE "Deposit successful."
ELSE
WRITE "Invalid amount."
END IF
3 :
WRITE "Enter amount to withdraw:"
READ amount
IF amount > 0 AND amount ≤ balance THEN
balance ← balance - amount
WRITE "Withdrawal successful."
ELSE
WRITE "Insufficient funds or invalid amount."
END IF
4 :
running ← FALSE
OTHERWISE :
WRITE "Invalid option – please try again."
END CASE
END WHILE
WRITE "Thank you for using the ATM."
Justification (example): The ATM menu decision is based on a single variable (option) that can take four discrete constant values. A CASE lists each possible choice clearly, improves readability, and makes it easy to add further options later. Because no range tests are required, CASE is more efficient and earns higher marks than an equivalent IF‑ELSE chain.
OTHERWISE clause for robustness and to meet AO2/AO3 criteria.END CASE.Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.