Use pseudocode to write: a 'CASE' structure

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Constructs: CASE Structure

11.2 Constructs – The CASE Structure

In this section you will learn how to use the CASE (also called switch) construct in pseudocode. The CASE structure provides a clear way to select one of many alternatives based on the value of a single expression.

When to use CASE

  • The decision depends on the value of a single variable or expression.
  • There are three or more possible distinct values.
  • Each value requires a different block of statements.

General syntax

ComponentPseudocode
Start of CASECASE expression OF
First option value1 :
  statements
Additional options value2 :
  statements
Default option OTHERWISE :
  statements
End of CASEEND CASE

All statements belonging to a particular option are indented beneath the colon. The OTHERWISE clause is optional but recommended to handle unexpected values.

Step‑by‑step construction

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

Example: Grade classification

Suppose we have a variable mark that contains a student's exam mark (0–100). We want to assign a letter grade according to the following rules:

  • 90–100 → A
  • 80–89 → B
  • 70–79 → C
  • 60–69 → D
  • 0–59 → F

Because the ranges are not single values, we first convert the mark to a “grade band” using integer division, then apply a CASE structure.

READ mark

SET band ← mark DI \cdot 10 // integer division

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

Note the use of DIV to obtain the tens digit, which makes the CASE values simple integers.

Comparison with IF…ELSE IF…ELSE

AspectIF…ELSE IF…ELSECASE
Decision basisMultiple independent Boolean expressionsSingle expression compared to constant values
ReadabilityCan become lengthy with many conditionsCompact when many discrete values are possible
EvaluationEach condition evaluated until one is trueExpression evaluated once; then a lookup
Default handlingELSE clauseOTHERWISE clause

Common mistakes

  • Forgetting the END CASE – leads to ambiguous block termination.
  • Using relational operators (e.g., >=) inside a CASE – CASE only matches exact values.
  • Omitting the OTHERWISE clause when an unexpected value could occur.
  • Mixing data types – the expression and the case values must be of the same type.

Practice exercise

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 (try to write it yourself first):

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 // invalid month

END CASE

WRITE days

Suggested diagram: Flow of control through a CASE structure – each value leads to a distinct branch, with OPTIONAL OTHERWISE handling the default case.

Key take‑aways

  • The CASE structure simplifies selection when a single expression can take many discrete values.
  • Always ensure the expression and case values share the same data type.
  • Include an OTHERWISE clause to make your pseudocode robust.
  • Remember to close the block with END CASE.