Multi-Way Selection (CASE OF ... OTHERWISE ... ENDCASE)

When an algorithm must branch across many mutually exclusive outcomes based on the value of a single variable, nesting dozens of IF ... THEN ... ELSE statements creates messy, hard-to-read code. Cambridge pseudocode provides the CASE OF statement for elegant multi-way branching.


1. Syntax of CASE OF

CASE OF <Identifier>
    <Value1> : <Statement>
    <Value2> : <Statement>
    <LowValue> TO <HighValue> : <Statement>
    <ValA>, <ValB>, <ValC> : <Statement>
    OTHERWISE : <Statement>
ENDCASE

Key Syntax Rules:

  1. Target Variable: Specified once in the CASE OF <Identifier> header.
  2. Selector Colons: Each matching value is followed immediately by a colon :.
  3. Range Support: You can test inclusive ranges using the keyword TO (e.g. 1 TO 10 : ...).
  4. List Support: You can test comma-separated discrete values (e.g. 'A', 'E', 'I', 'O', 'U' : ...).
  5. Fallback (OTHERWISE): Catches any input value that did not match any of the prior branches.
  6. Closing (ENDCASE): Required to terminate the structure.

2. Comparison: Nested IF vs CASE OF

Feature

Nested IF

CASE OF

Best Used For

Complex conditions involving multiple variables, relational operators (>, <), and compound logic (AND, OR).

Inspecting a single variable against specific constants or discrete ranges.

Readability

Tends to drift rightwards with deep indentation ("pyramid of doom").

Clean vertical alignment of branches.

Default Handling

Final ELSE block.

OTHERWISE block.

Termination Keyword

Multiple ENDIF statements matching each opened IF.

Single ENDCASE statement.

AI Socratic Tutor

All-Access
Sign in or create an account. AI Tutor is an All-Access benefit.
Sign Up

Need guidance? Ask a question or use the hint below.

AI Socratic Tutor is an All-Access Benefit

Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.

You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.

Exam-Style Check: CASE OF Range and Fallback Syntax

• Multiple Choice
+25 XP
Question

Which keyword is used in Cambridge pseudocode as the catch-all fallback clause in a CASE OF statement when none of the specified values or ranges match?

All-Access Required

Interactive exercises for Module 2 and beyond require an active plan.