Learning Objectives
- Use CASE OF for selection based on one identifiers value.
- Include an OTHERWISE branch where appropriate.
- Decide when CASE is clearer than IF.
- Trace a CASE statement accurately.
Key Terms
- CASE statement
- A selection structure that chooses one branch according to the value of one identifier.
- Case value
- A value matched against the controlling identifier.
- OTHERWISE
- An optional final branch used when no listed case matches.
- Controlling identifier
- The variable or identifier whose value determines the branch.

Purpose Of CASE
CASE is useful when one identifier can take several exact values and each value requires a different action. It avoids a long chain of equality tests and makes the choices easy to scan.
The structure compares the controlling identifier with the listed case values. The matching branch executes, then control continues after ENDCASE.
CASE is not normally suitable for complex ranges or conditions involving several variables. An IF statement is clearer for those situations.
Cambridge CASE Syntax
The case values should usually be single values. The OTHERWISE branch handles any value not explicitly listed and is especially useful for invalid input.
Each branch is shown as one statement in the standard format. If more complex work is required, the branch can call a procedure, preserving readability.
CASE OF MenuChoice
1 : OUTPUT "Add record"
2 : OUTPUT "Edit record"
3 : OUTPUT "Delete record"
OTHERWISE OUTPUT "Invalid choice"
ENDCASE
CASE Compared With IF
| Use CASE When | Use IF When |
|---|---|
| One identifier is compared with exact values | Conditions use ranges such as Mark >= 70 |
| Choices are discrete menu codes or characters | Several variables are involved |
| Many equality branches would make IF repetitive | Different relational or logical operators are needed |
Characters And Strings As Case Values
A menu choice may be an INTEGER or CHAR. For example, a user may enter A, S or Q. The type of each case value must be compatible with the controlling identifier.
Case matching is exact. Lower-case q does not necessarily match upper-case Q. The program can use UCASE before the CASE to normalise input when required.
Using Procedures In CASE Branches
Calling procedures keeps each case branch short and gives each operation a meaningful name. This also supports maintainability.
CASE OF Choice
1 : CALL AddStudent
2 : CALL FindStudent
3 : CALL DisplayAll
OTHERWISE OUTPUT "Choose 1, 2 or 3"
ENDCASE
Worked Examples
Day Number
Question: Output Weekend for day numbers 6 and 7, otherwise Weekday for 1 to 5 and Invalid for anything else.
- A simple CASE can list each exact value.
- Values 1 to 5 may each output Weekday.
- Values 6 and 7 output Weekend.
- OTHERWISE handles all other values.
Answer: Use CASE OF Day with branches 1 to 5 for Weekday, 6 and 7 for Weekend, and OTHERWISE for Invalid.
Normalising A Command
Question: How can a program make q and Q select the same CASE branch?
- Convert the input to upper case using UCASE.
- Use the converted value as the controlling identifier.
- List Q as the case value.
Answer: Command <- UCASE(Command), then CASE OF Command with a Q branch.
Examination Guidance
- Use CASE for exact values of one identifier.
- Include OTHERWISE when unlisted or invalid values are possible.
- Use IF for ranges or compound conditions.
- Keep case branches simple, often by calling procedures.
- Match the data type and case of values.
Common Mistakes
- Writing conditions such as Mark >= 50 as case values.
- Forgetting ENDCASE.
- Assuming OTHERWISE is executed after every matching branch.
- Using CASE when several variables must be tested.
- Failing to handle unexpected menu choices.
Knowledge Check
1. What determines the branch in CASE?
2. What is OTHERWISE for?
3. When is IF preferable?
4. Can CASE be used with a CHAR?
5. Why call procedures from case branches?