Document Templates : DGL Statements : Control Flow Statements : SELECT/WHEN Statement

SELECT/WHEN Statement

The SELECT/WHEN construct is used for conditional execution of DGL statements. This statement is more powerful than the previous IF/THEN/ELSE statement, in that it allows you to systematically list multiple conditions for statement execution.

Statement Syntax:

SELECT [selection_mode]

WHEN trigger => statements
[ WHEN trigger => statements ]
.
.
[ WHEN ANY => statements ]
.
[ WHEN trigger => statements ]
.
.
[ WHEN ANY => statements ]
.
.
[ OTHERWISE => statements ]

END SELECT ;

The optional selection_mode can be either the keyword FIRST or ANY. The selection-mode determines the way the statements are checked for possible execution - this will be explained shortly. The default selection-mode is first.

Note that WHEN statements are composed of two parts: the trigger to the left of the arrow, and statements on the right side of the arrow. The trigger is any valid Boolean expression. The statements following a trigger are performed only when the trigger is true. Whether or not these statements are actually executed also depends upon the selection-mode, as follows:

If the selection-mode is ANY, then the statements are executed whenever their corresponding trigger is true.
If the mode is FIRST (or not given), then only the first true trigger in the entire SELECT construct is executed; the rest are ignored, regardless of whether their triggers are true or not.
The WHEN ANY statements are executed when one or more of the preceding WHEN statements have been executed.
The OTHERWISE statements are executed only if no WHEN statement within the SELECT construct is triggered.

To demonstrate the execution of the SELECT/WHEN construct, consider the following example. a, b, and c are numeric variables.

SELECT ANY
WHEN a = 5 => b := 10 ;
WHEN a > b => b := 10 ;
WHEN a = 0 => b := 0 ;
WHEN ANY => write (’a may influence b’) ;
WHEN c = 5 => b := 5 ;
WHEN c > b => b := 5 ;
WHEN c = 0 => b := 0 ;
WHEN ANY => write (’c may influence b’) ;
OTHERWISE => write (’b has not been changed’) ;
END SELECT;

The execution is determined by:

1.
Each WHEN statement is triggered if its corresponding expression is evaluated to true.
2.
The first WHEN ANY statement is triggered if a is equal to 5, greater than b, or equal to zero.
3.
The second WHEN ANY statement is triggered if at least one of these same conditions is true with respect to the variable c instead of a.
4.
The OTHERWISE statement is triggered only if the value of b has not been changed within the SELECT statement’s evaluation.

When processing a WHEN ANY statement, the Documentor Tool only “looks back” to the previous WHEN ANY construct (if one exists). Therefore, in the above example, if a = 0 and none of the tests of c were true, the WRITE statement’s message c may influence b is not issued.

In this example, what would happen if the selection mode was FIRST instead of ANY, and the conditions a > b and c = 5 were both true ?

In this case the assignment b: = 10 and the first write message (the corresponding WHEN ANY statement) are executed. The assignment of b to 5 along with its corresponding WHEN ANY statement are not done because c = 5 is not the first true trigger.

The statements following the => symbol in the WHEN constructs may be any valid DGL statements. You may even enter a SELECT construct at this point. This allows you to nest SELECT constructs. There is no limit to the depth of nested SELECT blocks.