Skip to main content

CASE

CASE(expression, caseval => value, [... , caseval => value ] [, elsevalue ] )

expressionAn expression that results in a single value.
casevalA value to compare against the result of the expression.
=>The "results in" operator--valid only in CASE, MAP and CHOOSESETS.
valueThe value to return. This may be any expression or action.
elsevalueOptional. The value to return when the result of the expression does not match any of the caseval values. May be omitted if all return values are actions (the default would then be no action), or all return values are record sets (the default would then be an empty record set).
Return:CASE returns a single value, a set of values, a record set, or an action.

The CASE function evaluates the expression and returns the value whose caseval matches the expression result. If none match, it returns the elsevalue.

There may be as many caseval => value parameters as necessary to specify all the expected values of the expression (there must be at least one). All return value parameters must be of the same type.

Example:

MyExp := 1+2;
MyChoice := CASE(MyExp, 1 => 9, 2 => 8, 3 => 7, 4 => 6, 5);
  // returns a value of 7 for the caseval of 3
MyRecSet := CASE(MyExp, 1 => Person(per_st = 'FL'),
    2 => Person(per_st = 'GA'),
    3 => Person(per_st = 'AL'),
    4 => Person(per_st = 'SC'),
    Person);
  // returns set of Alabama Persons for the caseval of 3
MyAction := CASE(MyExp, 1 => FAIL('Failed for reason 1'),
    2 => FAIL('Failed for reason 2'),
    3 => FAIL('Failed for reason 3'),
    4 => FAIL('Failed for reason 4'),    FAIL('Failed for unknown reason'));
  // for the caseval of 3, Fails for reason 3

See Also: MAP, CHOOSE, IF, REJECTED, WHICH