do select, case

control structure

Syntax
do select numeric-expression
(case selector (| selector)*
     local-declaration*
     action*)+
( else
    local declaration*
    action*)?
done


Purpose

Evaluates a numeric expression, compares it against the numeric values or ranges in the case alternatives, then selects the alternative that applies. It is an error to have more than one case alternative apply to the same value.

If none of the case alternatives apply, and there is an else declaration, then the else declaration is selected. Otherwise, none of the parts are selected.

If a part was selected, the actions in that part are performed. Otherwise, none of the actions are performed.

The numeric ranges are inclusive. If the value falls between the upper and lower bounds, or matches either bound, the test succeeds and that case alternative is selected.

It is an error for a selecting value to match more than one case alternative. Each range set in a do select must describe a set of values separate from those in all the other range sets. This provision provides error detection, avoids "what happens when" questions, and allows for optimization.

This code sample shows how do select is used to evaluate a numeric expression, and then compare that expression against the numeric values or ranges in the case alternative, in order to select the alternative that applies.

If none of the case alternatives apply and there is an else part, then the else is selected.

  local integer val
  ...
  do select val
  case 1 | 3 | 5
    ; do something
  case 2 | 4 | 6 to 10
    ; do something else
  else
    ; do something different
  done