CASE Pseudocode
CASE Pseudocode
4 (b))
CASE statements allow one out of several branches of code to be executed, depending on the value of a variable.
CASE OF <identifier>
<value 1> : <statement>
<value 2> : <statement>
...
OTHERWISE <statement>
ENDCASE
It is best practice to keep the branches to single statements as this makes the pseudocode more readable. Similarly,
single values should be used for each case. If the cases are more complex, the use of an IF statement, rather than a
CASE statement, should be considered.
Each case clause is indented by two spaces. They can be considered as continuations of the CASE statement rather
than new statements.
Note that the case clauses are tested in sequence. When a case that applies is found, its statement is executed, and
the CASE statement is complete. Control is passed to the statement after the ENDCASE. Any remaining cases are
not tested.
If present, an OTHERWISE clause must be the last case. Its statement will be executed if none of the preceding
cases apply.
INPUT Move
CASE OF Move
ꞌWꞌ : Position ← Position – 10
ꞌEꞌ : Position ← Position + 10
ꞌAꞌ : Position ← Position – 1
ꞌDꞌ : Position ← Position + 1
OTHERWISE OUTPUT "Beep"
ENDCASE
41