Use pseudocode to write: a 'CASE' structure

11.2 Constructs – The CASE Structure

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.


1. When to use a CASE structure

  • The decision depends on the value of a single expression or variable.
  • There are three or more distinct constant values to test.
  • Each constant requires a different block of statements.
  • The values are discrete constants (integers, characters or strings) – not ranges or relational expressions.

Decision‑tree for choosing the most appropriate selection construct

QuestionRecommended 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)


2. Exam‑style formatting checklist

  • Keywords are written in UPPERCASE (CASE, OF, OTHERWISE, END CASE).
  • Each value : line begins with a single space before the value and a single space before the colon.
  • All statements belonging to a case are indented (four spaces or one tab) beneath the colon.
  • Terminate the block with END CASE on its own line.
  • Use the assignment arrow and the integer‑division operator DIV exactly as shown in the Cambridge Pseudocode Guide.


3. General syntax of a CASE structure

ComponentPseudocode
Start of CASECASE expression OF
First option  value1 :
        statements
Additional options  value2 :
        statements
Default option (optional)  OTHERWISE :
        statements
End of CASEEND 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.


4. Step‑by‑step construction of a CASE

  1. Identify the expression that determines the choice (single variable or simple calculation).
  2. Write CASE expression OF on a new line.
  3. For each possible constant value, write the value followed by a colon, then list the statements to be executed (indented).
  4. Optionally add an OTHERWISE clause for any values not explicitly listed.
  5. Terminate the structure with END CASE.


5. Data‑type considerations

  • The expression and every case value must share the same data type. Mixing types (e.g. CASE month OF ‘Jan’:) is a syntax error and loses marks.
  • Allowed data types for CASE in the syllabus are integer, character and string.

Valid examples

CASE month OF                // integer expression

1 : days ← 31

2 : days ← 28

END CASE

CASE grade OF // character expression

'A' : comment ← "Excellent"

'B' : comment ← "Good"

OTHERWISE : comment ← "Other"

END CASE


6. Why choose CASE over IF‑ELSE? (AO2/AO3 justification)

When analysing a problem you should consider:

  • Readability – A CASE presents a clear list of discrete alternatives, making the algorithm easier to read and to mark.
  • Efficiency – Many compilers/interpreters can implement CASE as a jump table, which is faster than evaluating a chain of IF‑ELSE tests.
  • Maintainability – Adding, removing or re‑ordering options only requires editing one line per case, whereas IF‑ELSE chains become harder to manage.
  • Exam marking – The Cambridge mark‑scheme awards higher marks for a correctly structured CASE when the problem fits the “single variable, many constants” pattern.

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.


7. Common mistakes and how to avoid them

  • Missing END CASE – always close the block.
  • Using relational operators inside CASE (e.g. >= 90) – CASE matches exact values only.
  • Omitting OTHERWISE when an invalid input is possible – leads to undefined behaviour.
  • Data‑type mismatch between the expression and case values.
  • Incorrect integer‑division operator – use DIV (or \ in some textbooks) to obtain the tens digit, not “÷ · 10”.
  • Incorrect indentation or colon placement – follow the exam‑style checklist in Section 2.


8. Example 1 – Grade classification (integer division)

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 mark

SET 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

Why DIV is used

DIV 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.


9. Example 2 – Menu‑driven program (CASE inside a loop)

This example shows a CASE nested inside a pre‑condition loop. The loop repeats the menu until the user selects “Exit”.

SET running ← TRUE

SET 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."


10. Example 3 – Nested CASE (hierarchical decision)

A two‑level menu: first choose a module, then choose an operation within that module.

READ module                // 1 = Maths, 2 = Science

CASE 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).


11. Reference to the Cambridge Pseudocode Guide

All syntax follows the Cambridge Pseudocode Guide (see https://www.cambridgeinternational.org/support-and-training/pseudocode-guide). In particular:

  • Assignment uses the arrow .
  • Integer division uses DIV.
  • Keywords are capitalised.
  • Indentation is significant for marking clarity.


12. Practice exercises

Exercise 1 – Days in a month (AO1 + AO2)

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.

  1. Read the value of month.
  2. Use a CASE structure to assign the correct number of days to the variable days.
  3. Output days.

Suggested solution

READ month

CASE 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

Exercise 2 – Design a simple ATM menu (AO3)

Scenario: A bank wants a console‑based ATM program that repeatedly offers the following options:

  1. Check balance
  2. Deposit money
  3. Withdraw money
  4. Exit

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:

  • Initialise balance ← 0 and a Boolean running ← TRUE.
  • Inside the loop, read the user's choice into option.
  • Use a CASE on option to perform the required action.
  • Include an OTHERWISE clause to handle invalid selections.
  • Terminate the loop when the user selects option 4.

Sample answer (students may vary):

SET balance ← 0

SET 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.


13. Key take‑aways

  • The CASE structure is ideal when a single expression can take many discrete constant values (≥ 3).
  • All case values must be the same data type as the expression.
  • Always include an OTHERWISE clause for robustness and to meet AO2/AO3 criteria.
  • Follow the exam‑style formatting checklist to avoid syntax‑related mark loss.
  • Close the block with END CASE.
  • CASE can be nested inside loops or other CASEs – use this to demonstrate good structural design (AO3).
  • When analysing a problem, explicitly state why CASE is the most appropriate construct (readability, efficiency, maintainability).