pTAL Reference Manual (H06.08+)

Expressions
HP pTAL Reference Manual523746-006
5-22
CASE
All fixed-point data types are compatible with each other; however, if
expression-1 and expression-2 are differently scaled fixed-point
expressions, the data type of the IF expression is the data type of the fixed-point
expression whose scale factor is largest. The smaller operand is implicitly scaled to
match the type of the IF expression. For example, these return statements are
equivalent:
The compiler numbers the instances of expression-1 consecutively, starting with 0.
If selector matches the compiler-assigned number of an expression-1, that
expression-1 is evaluated and becomes the result of the CASE expression. If
selector does not match a compiler-assigned number, expression-2 is
evaluated.
You can nest CASE expressions. CASE expressions resemble unlabeled CASE
statements except that CASE expressions select expressions rather than statements.
Example 5-4 on page 5-22 selects an expression based on the value of a and assigns
it to x:
int i;
fixed(2) f2;
fixed(3) f3;
fixed(3) proc p;
begin
return if i then f2
else f3;
end
int i;
fixed(2) f2;
fixed(3) f3;
fixed(3) proc p;
begin
return if i then $scale(f2,1);
else f3;
end
Example 5-4. CASE Expression
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.