beautypg.com

Example 4–9 procedure that uses the case statement – Compaq DEC Text Processing Utility AA-PWCBD-TE User Manual

Page 88

background image

Lexical Elements of the DEC Text Processing Utility Language
4.9 Reserved Words

Syntax

CASE case-selector [[FROM
lower-constant-expr, TO upper-constant-expr]]

[constant-expr_1 [[,...]]] : statement [[,...]];
[constant-expr_2 [[,...]]] : statement [[,...]];

.
.
.

[constant-expr_n [[,...]]] : statement [[,...]];

[[[INRANGE] : statement [[,...]] ;]]

[[[OUTRANGE] : statement [[,...]] ;]]

ENDCASE;

The single brackets are not optional for case constants. Example 4–9 shows how
to use the CASE statement in a procedure.

CASE constant expressions must evaluate at compile time to either a keyword,
a string constant, or an integer constant. All constant expressions in the CASE
statement must be of the same data type. There are two special case constants
in DECTPU: INRANGE and OUTRANGE. INRANGE matches anything that
falls within the case range that does not have a case label associated with it.
OUTRANGE matches anything that falls outside the case range. These special
case constants are optional.

FROM and TO clauses of a CASE statement are not required. If FROM and
TO clauses are not specified, INRANGE and OUTRANGE labels refer to data
between the minimum and maximum specified labels.

Example 4–9 shows a sample procedure that uses the CASE statement.

Example 4–9 Procedure That Uses the CASE Statement

PROCEDURE grades

answers := READ_LINE ("Enter number of correct answers:",5);
answers := INT (answers);

CASE answers FROM 0 TO 10

[10] : score := "A+";

[9] : score := "A";
[8] : score := "B";
[7] : score := "C";
[6] : score := "D";

[0,1,2,3,4,5] : score := "F";

[OUTRANGE] : score := "Invalid entry.";

ENDCASE;

MESSAGE (score);

ENDPROCEDURE;

This CASE statement compares the value of the constant selector answers to
the case labels (the numbers 0 through 10). If the value of answers is any of
the numbers from 0 through 10, the statement to the right of that number
is executed. If the value of answers is outside the range of 0 through 10, the
statement to the right of [OUTRANGE] is executed. The value of score is written
in the message area after the execution of the CASE statement.

4–22 Lexical Elements of the DEC Text Processing Utility Language