LOOP

Use the LOOP statement to define a loop with an indeterminate number of iterations.

Syntax

(1)
Range

1  left_expression TO right_expression?  STEP increment_expr
Notes:

Element Description Restrictions Syntax
expression Value to compare with loop_var Must match loop_var data type Expression
increment_expr Positive or negative value by which loop_var is incremented Must return an integer. Cannot return 0. Expression
label Name of the loop label for this loop Must be unique among labels in this SPL routine Identifier
left_expression Starting expression of a range Value must match SMALLINT or INT data type of loop_var Expression
loop_var Variable that determines how many times the loop executes Must be defined and in scope within this statement block Identifier
right_expression Ending expression in the range Same as for left_expression Expression

Usage

The LOOP statement is an iterative statement that resembles the FOR and WHILE statements. Like FOR and WHILE, the LOOP statement can have an optional loop label, It can include the CONTINUE statement to specify another iteration, and the EXIT statement to terminate execution of the loop.

Besides resembling FOR and WHILE in its functionality, the LOOP statement can use the syntax of FOR or WHILE that precedes the statement block. Sections that follow describe several forms of the LOOP statement, including these:
  • Simple LOOP statements that iterate a statement loop indefinitely
  • FOR LOOP statements, that use FOR statement syntax specify a finite number of iterations
  • WHILE LOOP statements, that iterate while a specified condition is true
  • Labeled versions of each of these LOOP statements, which can terminate deeply nested loops.