TAL Programmer's Guide
CASE Expression
Using Special Expressions
096254 Tandem Computers Incorporated 13–3
CASE Expression The CASE expression selects one of several expressions. You can nest CASE
expressions. To use a CASE expression, specify:
CASE selector OF
Specifies an INT arithmetic expression that selects the expression to evaluate.
BEGIN expressions;
Specifies a choice of one or more expressions. Separate successive expressions with
semicolons. Each expression must be either:
An INT arithmetic expression
A conditional expression (excluding a relational operator with no operands),
the result of which has data type INT.
The compiler numbers each BEGIN expression consecutively, starting with 0.
When the selector matches the compiler-assigned number of a BEGIN expression,
the expression is evaluated. The result of expression becomes the result of the
overall CASE expression.
OTHERWISE expression; (optional)
Specifies an expression to evaluate if the selector does not select one of the BEGIN
expressions. The OTHERWISE expression and the BEGIN expressions must have all
the same data type. If you omit the OTHERWISE clause and an out-of-range case
occurs, results are unpredictable.
END
Specifies the end of the CASE expression.
For example, you can use a CASE expression to select the value resulting from one of
several expressions and assign it to variable X. The expression selected depends on the
value of selector A:
INT x, a, b, c, d;
!Code to initialize variables
x := CASE a OF
BEGIN
b; !If A is 0, assign value of B to X.
c; !If A is 1, assign value of C to X.
d; !If A is 2, assign value of D to X.
OTHERWISE –1; !If A is any other value,
END; ! assign –1 to X.
The CASE expression resembles the unlabeled CASE statement except that the CASE
expression selects an expression, while the unlabeled CASE statement selects a
statement. (The unlabeled CASE statement is described in the TAL Reference Manual.)