Selection: CASE
CASE OF picks one branch
- A
CASEruns the branch whose value matches the variable. - Branches are tested in order. The first match runs, and the rest are skipped.
OTHERWISEis the branch for every other value. It comes last.
DECLARE Move : CHAR
Move ← 'W'
CASE OF Move
'W' : OUTPUT "west"
'E' : OUTPUT "east"
OTHERWISE : OUTPUT "other"
ENDCASE
A range of values, and longer branches
- In 9618, one clause can cover a range:
<value1> TO <value2> :. Both ends count. - A clause can run several statements. They run until the next value,
OTHERWISEorENDCASE. - The order still matters. If two ranges overlap, only the first one that matches runs.
DECLARE Mark, Passes : INTEGER
Mark ← 64
Passes ← 0
CASE OF Mark
70 TO 100 : OUTPUT "Distinction"
Passes ← Passes + 1
50 TO 69 : OUTPUT "Pass"
Passes ← Passes + 1
OTHERWISE : OUTPUT "Not yet"
ENDCASE
OUTPUT "Passes: ", Passes
The output is Pass, then Passes: 1. A mark of 70 gives Distinction, because 70 is an end of the first range.
Watch out
- A character value uses single quotes:
'W'. - Many short branches read well as a
CASE. Conditions such asMark > 50 AND Absent = FALSEneed anIF. - IGCSE 0478 pseudocode has no ranges and keeps one statement per branch. In 9618 both are allowed.
Common mistakes
- Close the block with
ENDCASE. - Put a colon after the value, and after
OTHERWISE. - Write a range low to high:
50 TO 69, not69 TO 50. A backwards range matches nothing. - Leave no gaps between ranges. With
70 TO 100and50 TO 68, a mark of 69 falls toOTHERWISE.
Now you try
- Read a character and choose a word with
CASE OF.
Independent transfer
The final task has no supplied algorithm. Plan its inputs, processing and outputs, then solve it before opening support. Check more than the sample case.
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Read a CHAR Move. Output west for W, east for E, and other for anything else.
Click Run to see the output here.
Difficulty 2/5 · 140 coins + a small bonus on first completion today
Read an integer Mark from 0 to 100. Use one CASE with ranges to output A for 70–100, B for 50–69 and U for anything lower.
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Read integer Rank. Using CASE, output gold for 1, silver for 2, bronze for 3, otherwise unplaced.
Click Run to see the output here.