DGL Statement Reference : SELECT/WHEN

SELECT/WHEN

Description
Performs conditional execution of DGL statements. This statement is more powerful than the IF/THEN/ELSE statement in that it allows you to systematically list multiple conditions for statement execution.
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 on the value of the selection_mode. In addition:

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.

Regardless of the mode, when all of the triggers are false, the tool will execute the OTHERWISE clause, if it exists.
Syntax
SELECT [selection_mode]

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

END SELECT ;
Parameters
 
Specifies the selection mode, which determines the way the statements are checked for possible execution. The possible values are as follows:
FIRST—Only the first true trigger in the entire SELECT construct is executed; the rest are ignored, regardless of whether their triggers are TRUE. This is the default value.
ANYIf the selection mode is ANY, the statements are executed whenever their corresponding trigger is TRUE.
Notes
Both the WHEN ANY and the OTHERWISE statements are optional.
The Documentor permits the nesting of SELECT/WHEN constructs.
Example
Consider the following example, where 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 the following:

Each WHEN statement is triggered if its corresponding expression is evaluated to TRUE.

The first WHEN ANY statement is triggered if a is equal to 5, greater than b, or equal to zero. The second WHEN ANY statement is triggered if at least one of the previous conditions is true with respect to the variable c instead of a.

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 only “looks back” to the previous WHEN ANY construct (if one exists). Therefore, in this 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.

Using the same example, what would happen if the selection mode is FIRST instead of ANY, and the conditions a > b and c = 5 are 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 can be any valid DGL statements. You can 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.

See Also