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:

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