Conditional Logic

CASE...OF

When a single variable can take one of many known values, a CASE...OF statement is the cleanest way to choose what to do. It is the multi-way equivalent of IF...THEN...ELSE — clearer and shorter than a long chain of nested IFs.

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"
ENDCASE

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

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"
ENDCASE

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

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"
ENDCASE

Without OTHERWISE

CASE OF Grade
  'A' : OUTPUT "Excellent"
  'B' : OUTPUT "Good"
  'C' : OUTPUT "Pass"
ENDCASE

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

6.4 CASE...OF vs Nested IF

Both CASE...OF and nested IF can express multi-way decisions, but they shine in different situations.

FeatureCASE...OFNested IF
Tests how many variables?One onlyMany (one per level)
Best forExact value matchingRanges and complex conditions
Readability with many branchesVery clean — one line per caseGets deeply indented and hard to follow
Default / fallbackOTHERWISEFinal ELSE
End keywordENDCASEENDIF (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"
ENDCASE

Using 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
ENDIF

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

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"
ENDCASE

Case 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"
ENDCASE

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

6.6 Key Points Summary

Here are the most important facts about CASE...OF before you tackle the Question Bank.

CASE...OF makes a multi-way decision based on the value of ONE variable.
Syntax: CASE OF <variable> ... <value> : <statement> ... ENDCASE.
Each case label is followed by a colon ":" before its statement(s).
OTHERWISE is optional and runs when no case label matches.
ENDCASE is mandatory and closes the structure.
Best for exact value matching (integers, chars, strings).
For ranges (>= 50), nested IF is usually a better choice.
Multiple statements can follow a single case label.
CASE...OF can be nested inside another CASE...OF or inside IF/loops.
Including an OTHERWISE is good defensive practice to handle unexpected input.

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.

Question Bank

Test yourself — 12 questions including True/False and code-tracing problems.

0/12 answered

Question 1Multiple Choice

What is the main purpose of a CASE...OF statement?

Question 2True / False

Every CASE...OF statement must be closed with the ENDCASE keyword.

Question 3Multiple Choice

Which line correctly begins a CASE...OF statement in CIE pseudocode?

Question 4True / False

The OTHERWISE clause is mandatory in every CASE...OF statement.

Question 5Multiple Choice

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

Question 6True / False

In CIE pseudocode, each case label is followed by a colon ":".

Question 7Multiple Choice

When is CASE...OF usually preferred over nested IF?

Question 8Multiple Choice

DECLARE Choice : INTEGER
Choice <- 9
CASE OF Choice
  1 : OUTPUT "New"
  2 : OUTPUT "Open"
  3 : OUTPUT "Save"
ENDCASE

Question 9True / False

A CASE...OF statement can only test ONE variable at a time.

Question 10Multiple Choice

Where must the OTHERWISE clause appear in a CASE...OF?

Question 11True / False

A CASE...OF can be nested inside another CASE...OF.

Question 12Multiple Choice

DECLARE Grade : CHAR
Grade <- 'B'
CASE OF Grade
  'A' : OUTPUT "Excellent"
  'B' : OUTPUT "Good"
  'C' : OUTPUT "Pass"
  OTHERWISE OUTPUT "Fail"
ENDCASE

Answer all 12 questions to enable submission.