SELECT CASE

The SELECT CASE instruction executes one of several groups of statements, depending on the value of an expression. The syntax for SELECT CASE is:
SELECT CASE test-expr
CASE expr
statements-1
CASE value TO value
statements-2
CASE [IS] compare-operator expr
statements-2
CASE expr-list
statements-2
CASE ELSE
statements-3
END SELECT

where:

  • test-expr is the expression to compare with each CASE expression. This can be a numeric or string expression.
  • if expr equals the test-expr, the case is performed.
  • value TO value is a range of values to compare with the test-expr. This range can be a numeric or string expression. If a string expression is used, message PDR0812W will appear. It may be ignored.
  • compare-operator expr is a range of values, such as IS > 10. The IS keyword is optional.
  • expr-list is one or more valid case expressions separated by commas. The correct form is:

    case-expr [, case-expr [, case-expr]...]

    An example of expr-list is:
    SELECT CASE %%Value
    CASE 1
    %%Sale = 'single' 
    CASE 2
    %%Sale = 'double' 
    CASE 3, 5 TO 7, IS > 20
    %%Sale = 'special'
    CASE ELSE
    %%Sale = 'other'
    END SELECT
    In this example:
    • if the variable %%Value is 1, the variable %%Sale is set to single
    • if %%Value is 2, %%Sale is set to double
    • if %%Value is 3, 5, 6, 7, or greater than 20, %%Sale is set to special
    • if %%Value is anything else, %%Sale is set to other.

From this example, you can see that the SELECT CASE instruction can be used in place of an IF THEN ELSE instruction with multiple ELSEIF statements.