switch Instruction
The syntax of the switch instruction is as follows.
switch expression do
[case constant‑expression {, constant‑expression}:
statement‑list]
[default: statement‑list]
endswitch;
Description
The switch expression must evaluate to a primitive type value. The following primitive types are not supported.
The switch expression is evaluated once only, at the beginning of the switch instruction execution. Each case constant is compared against this evaluated expression.
Each case statement can consist of one or more constant‑value expressions that are to be matched against the switch expression.
When a case statement value matches the evaluated switch expression value, the statement‑list body is executed.
You can optionally define a default statement that includes a statement-list to be executed if no case values match the evaluated switch expression value.
Behavior
When using the switch instruction:
-
At most, one case block is executed.
-
Case constant values must be compile-time constant value expressions. This means the value can be a simple literal value or an expression constructed using literals and other constants. These could be method local constants, type constants, or global constants.
-
The type of the case statement values must be assignment‑compatible with the switch expression type; for example, a String switch expression could have a mixture of Character and String case values. These will all be evaluated and compared as String values.
-
The case constant values must be unique across all cases in the same switch statement. If two case constants have a matching value, a 6051 (Duplicated identifier) compile error occurs.
-
The switch statement can have zero or more case labels and at most, one default label. A 6465 (Empty Switch statement) compile error will occur if the switch statement does not contain any blocks.
-
The default label must be the last block of the switch statement.
-
If no case matches and no default is present, control passes to the statement immediately following the endswitch.
-
There is no implicit or explicit fall‑through between case statements.
Examples
The following is an example of the switch instruction.
vars
item: String;
begin
item := CatInTheHat@pullSomethingFromHat();
switch item do
case "cake":
write "The Cat pulls out a cake and balances it high!";
case "fan", "rake":
write "A fan or a rake appears — both cause quite a mess!";
case "game":
write "The Cat pulls out a crazy game that fills the house with fun!";
case "boat":
write "He reveals a toy boat, sailing it across the floor.";
default:
write "The Cat reaches in... but nothing comes out this time.";
endswitch;
end;
The above example has the following behavior listed in the following table.
| On pulling... | The method would output... |
|---|---|
| A “cake” from the hat | The Cat pulls out a cake and balances it high! |
| A “fan” or a “rake” from the hat | A fan or a rake appears — both cause quite a mess! |
| ''" | The Cat reaches in... but nothing comes out this time. |
2025.0.01 and higher
