[Next] [Prev] [Right] [Left] [Up] [Index] [Root]
The case statement

The case statement

Magma provides a case-statement and a case-expression for multi-branching conditionals.

The case-statement is a conditional statement. It causes execution of some statements to depend on the value of a certain expression.

The syntax of the case-statement is:

case EXPRESSION:
    when EXPRESSION, ... EXPRESSION:
        STATEMENTS
    ...
    when EXPRESSION, ... EXPRESSION:
        STATEMENTS
    else
        STATEMENTS
end case;

The section `else STATEMENTS' is optional.

To execute the case-statement, Magma evaluates the expression following `case'. Magma then evaluates each of the expressions following a `when' until it finds an expression whose value is equal to that of the `case' expression. If such an expression is found then the statements following it are executed, and then the case-statement has finished. If none of the values of the `when' expressions equals the value of the `case' expression, then the statements following `else' are executed. If no action is desired in the latter case, the section `else STATEMENTS' may be omitted.

Example

> case Random(1, 3):
>     when 1:
>         print "Hello.";
>     when 2:
>         print "G'day.";
>     else
>         print "Pleased to meet you.";
> end case;
Pleased to meet you.

[Next] [Prev] [Right] [Left] [Up] [Index] [Root]