Table of Contents Previous Next



4.5.6 Loops
With the LOOP, EXIT, CONTINUE, WHILE, and FOR statements, you can arrange for your SPL program to repeat a series of commands.
4.5.6.1 LOOP
statements
LOOP defines an unconditional loop that is repeated indefinitely until terminated by an EXIT or RETURN statement.
4.5.6.2 EXIT
EXIT [ WHEN expression ];
If WHEN is present, loop exit occurs only if the specified condition is true, otherwise control passes to the statement after EXIT.
EXIT can be used to cause early exit from all types of loops; it is not limited to use with unconditional loops.
4.5.6.3 CONTINUE
The CONTINUE statement provides a way to proceed with the next iteration of a loop while skipping intervening statements.
When the CONTINUE statement is encountered, the next iteration of the innermost loop is begun, skipping all statements following the CONTINUE statement until the end of the loop. That is, control is passed back to the loop control expression, if any, and the body of the loop is re-evaluated.
If the WHEN clause is used, then the next iteration of the loop is begun only if the specified expression in the WHEN clause evaluates to true. Otherwise, control is passed to the next statement following the CONTINUE statement.
The CONTINUE statement may not be used outside of a loop.
The following is a variation of the previous example that uses the CONTINUE statement to skip the display of the odd numbers.
4.5.6.4 WHILE
WHILE expression LOOP
statements
The WHILE statement repeats a sequence of statements so long as the condition expression evaluates to TRUE. The condition is checked just before each entry to the loop body.
The following example contains the same logic as in the previous example except the WHILE statement is used to take the place of the EXIT statement to determine when to exit the loop.
Note: The conditional expression used to determine when to exit the loop must be altered. The EXIT statement terminates the loop when its conditional expression is true. The WHILE statement terminates (or never begins the loop) when its conditional expression is false.
FOR name IN [REVERSE] expression .. expression LOOP
statements
This form of FOR creates a loop that iterates over a range of integer values. The variable name is automatically defined as type INTEGER and exists only inside the loop. The two expressions giving the loop range are evaluated once when entering the loop. The iteration step is +1 and name begins with the value of expression to the left of .. and terminates once name exceeds the value of expression to the right of ... Thus the two expressions take on the following roles: start-value .. end-value.
The optional REVERSE clause specifies that the loop should iterate in reverse order. The first time through the loop, name is set to the value of the right-most expression; the loop terminates when the name is less than the left-most expression.
The following example simplifies the WHILE loop example even further by using a FOR loop that iterates from 1 to 10.
Note: SPL also supports CURSOR FOR loops (see Section 4.8.7).


Table of Contents Previous Next