switch

Syntax

switch ( expression ) {
    case a : { BLOCK }
    . . .
    case p ,q, r : { BLOCK }
    case n: {BLOCK}
    default: {BLOCK}
}

Parameters

Parameter
Definition
a,b, … p,q,r, … n values indicating the value of the variable that will cause the corresponding BLOCK to be executed
expression PSL expression whose integer value specifies the PSL statement BLOCK that will be executed
BLOCK one or more statements that are executed when the corresponding case value equals variable

Description

The switch statement evaluates expression and based on its integer value executes a specific BLOCK. The case labels correspond to the values of expression for which a specific BLOCK is available.

If the value of expression falls outside the range of the values in the case labels, execution continues with the BLOCK corresponding to the default label. If no default label exists, execution will continue with the first statement following the switch statement.

The switch statement is similar in form and function to the C switch statement.

The switch statement executes in almost the same way as a long sequence of ifthenelseif statements. A case or default clause is effectively a run-time statement that specifies a comparison against the value of expression:

  • If the value of expression matches a case, execution moves inside the BLOCK for the case or default clause; and after completing BLOCK, execution continues after the entire switch statement (that is, there is no falling through to the next case clause).
  • If the value of expression does not match a case, execution skips to the default clause; and if there is none, execution moves to the statement following the switch statement.

Any statement within the switch statement case block that is not part of a case or default BLOCK executes only if all the case labels above it failed to match expression (that is, it executes as part of the normal sequence of control flow).