case: One setting of a switch

Package: language

Syntax

switch (expr) {
case val1 [, val1,...]:
    statements
case val3 [, val3,...]:
    statements
        (etc.)
default:
    statements
}

Elements

expr
An integer-valued expression tested before entry into the switch block.
valN
Integer valued constants used to match expression.
statements
Simple or compound statements to be executed when the appropriate case or default block is selected.

Description

The switch statement provides a multiway branch capability. The switch expression is evaluated and control branches to the matching case block. If there is no match the default block, if present, receives control. If no default block is present, the switch is skipped.

Each case statement consists of a list of values defining the case, and an executable statement (possibly compound) to be executed if the case is selected by the switch. Execution will continue until the next case is reached, at which time a branch out of the switch statement occurs. Note this difference from the C switch case, where an explicit break statement is required to exit a switch. If a break is used in a CL switch, it will act upon the loop statement containing the switch, not the switch itself.

Note that both the switch expression and the case constants may be integers, or single characters which are evaluated to their ASCII equivalents.

The default statement must be the last statement in the switch block.

Examples

1. Multiple cases, no default case.

switch (opcode) {
case 1:
    task1 (args)
case 2:
    task2 (args)
case 5:
    task5 (args)
}

2. Multiple values in a case.

switch (digit) {
case '1','2','3','4','5','6','7':
    n = n * 8 + digit - '0'
default:
    error (1, "invalid number")
}

Bugs

Only integer values are allowed (no strings). The case values must be constants; ranges are not permitted.

See also

if else, goto