6.1 What is CASE...OF?
- A CASE...OF statement is a multi-way selection structure.
- It compares ONE variable against a list of possible fixed values.
- For each value, you write the statement(s) that should run when the variable equals that value.
- If no value matches, the OTHERWISE branch (if present) runs.
Key idea: CASE...OF is best when you are matching a variable against exact values (1, 2, 3, "A", "B"...). For ranges (Marks >= 50), use nested IF instead.
A Real Example — Day of the Week
DECLARE Day : INTEGER
INPUT Day
CASE OF Day
1 : OUTPUT "Monday"
2 : OUTPUT "Tuesday"
3 : OUTPUT "Wednesday"
4 : OUTPUT "Thursday"
5 : OUTPUT "Friday"
6 : OUTPUT "Saturday"
7 : OUTPUT "Sunday"
OTHERWISE OUTPUT "Invalid day number"
ENDCASEIf the user enters 3, the program outputs "Wednesday". If they enter 9, none of the labels 1–7 match, so the OTHERWISE branch runs and prints "Invalid day number".
What is CASE...OF?
6.2 CASE...OF Syntax
The general shape of a CASE...OF in CIE pseudocode is:
CASE OF <variable>
<value 1> : <statement 1>
<value 2> : <statement 2>
...
<value n> : <statement n>
OTHERWISE <statement>
ENDCASE- CASE OF <variable> — opens the structure and names the variable to test.
- <value> : <statement> — one line per case. The colon separates the value from the action.
- OTHERWISE <statement> — optional fallback when no value matches.
- ENDCASE — closes the structure (mandatory).
Example: Menu Selection
DECLARE Choice : INTEGER
INPUT Choice
CASE OF Choice
1 : OUTPUT "New file"
2 : OUTPUT "Open file"
3 : OUTPUT "Save file"
4 : OUTPUT "Print file"
5 : OUTPUT "Exit"
OTHERWISE OUTPUT "Unknown option"
ENDCASEIndentation tip: Indent each case body by 2 spaces. It does not change execution but makes the structure crystal clear — examiners reward well-laid-out pseudocode.
CASE...OF Syntax
6.3 The OTHERWISE Clause
The OTHERWISE clause is the "catch-all" branch. It runs when the variable does not match any of the listed case values.
- OTHERWISE is optional but recommended in real programs.
- It must appear after all case labels, just before ENDCASE.
- Without OTHERWISE, a non-matching value means nothing runs — execution continues after ENDCASE.
- It plays the same role as ELSE in an IF statement.
Example: With and Without OTHERWISE
With OTHERWISE
CASE OF Grade
'A' : OUTPUT "Excellent"
'B' : OUTPUT "Good"
'C' : OUTPUT "Pass"
OTHERWISE OUTPUT "Unknown grade"
ENDCASEWithout OTHERWISE
CASE OF Grade
'A' : OUTPUT "Excellent"
'B' : OUTPUT "Good"
'C' : OUTPUT "Pass"
ENDCASEIf Grade is "F" in the first version, the OTHERWISE runs and prints "Unknown grade". In the second version, nothing happens — the program just moves past ENDCASE. Always including OTHERWISE is a defensive habit that prevents silent bugs.
Exam tip: If a question says "the program must always output something", you almost certainly need an OTHERWISE branch.
The OTHERWISE Clause
6.4 CASE...OF vs Nested IF
Both CASE...OF and nested IF can express multi-way decisions, but they shine in different situations.
| Feature | CASE...OF | Nested IF |
|---|---|---|
| Tests how many variables? | One only | Many (one per level) |
| Best for | Exact value matching | Ranges and complex conditions |
| Readability with many branches | Very clean — one line per case | Gets deeply indented and hard to follow |
| Default / fallback | OTHERWISE | Final ELSE |
| End keyword | ENDCASE | ENDIF (one per IF) |
Same Logic, Two Styles
Using CASE...OF
CASE OF Choice
1 : OUTPUT "Add"
2 : OUTPUT "Edit"
3 : OUTPUT "Delete"
OTHERWISE OUTPUT "Invalid"
ENDCASEUsing Nested IF
IF Choice = 1 THEN
OUTPUT "Add"
ELSE
IF Choice = 2 THEN
OUTPUT "Edit"
ELSE
IF Choice = 3 THEN
OUTPUT "Delete"
ELSE
OUTPUT "Invalid"
ENDIF
ENDIF
ENDIFRule of thumb: Use CASE...OF when comparing ONE variable against several fixed values. Use nested IF when the conditions involve ranges, multiple variables, or compound expressions with AND/OR.
CASE...OF vs Nested IF
6.5 Practical Examples
Let's trace three real-world CASE...OF programs step by step.
Example 1: Day of the Week
DECLARE Day : INTEGER
Day <- 3
CASE OF Day
1 : OUTPUT "Mon"
2 : OUTPUT "Tue"
3 : OUTPUT "Wed"
4 : OUTPUT "Thu"
5 : OUTPUT "Fri"
OTHERWISE OUTPUT "Weekend"
ENDCASE- Day is 3 → matches the third case label
- OUTPUT "Wed" runs
- Execution continues after ENDCASE — no other case runs
Example 2: Grade Description (string case labels)
DECLARE Grade : CHAR
Grade <- 'B'
CASE OF Grade
'A' : OUTPUT "Excellent"
'B' : OUTPUT "Good"
'C' : OUTPUT "Pass"
OTHERWISE OUTPUT "Fail"
ENDCASECase labels can be characters or strings, not just integers. Grade = 'B' matches the second case, so "Good" is output.
Example 3: Multiple Statements per Case
DECLARE Op : INTEGER
INPUT Op
CASE OF Op
1 : OUTPUT "Adding"
OUTPUT "Enter two numbers"
2 : OUTPUT "Saving"
OUTPUT "Enter filename"
3 : OUTPUT "Quitting"
OUTPUT "Goodbye"
OTHERWISE OUTPUT "Invalid operation"
ENDCASEEach case body can contain several statements. They all run when that case label matches, in the order they appear.
Worked trace: For Op = 2 the program outputs "Saving" then "Enter filename" — both statements under case 2. Other cases are skipped entirely.
Practical Examples
6.6 Key Points Summary
Here are the most important facts about CASE...OF before you tackle the Question Bank.
Exam tip: When a question shows a variable with several named, distinct values (1, 2, 3, "A", "B"), reach for CASE...OF. When it shows thresholds (>= 50, >= 75), reach for nested IF.