Navigation: Templates > Template Language Reference > Complete Alpha Listing >====== #LOOP (iteratively generate code) ====== | |
#LOOP [, | UNTIL( expression ) | ] | ||
WHILE( expression ) | ||||
FOR( counter, start, end ) [, BY( step ) ] | ||||
TIMES( iterations ) | ||||
statements | ||||
#ENDLOOP |
#LOOP | Initiates an iterative statement execution structure. |
UNTIL | Evaluates its expression before each iteration of the #LOOP. If its expression evaluates to true, the #LOOP control sequence terminates. |
expression | Any Template language expression which can evaluate to false (blank or zero) or true (any other value). |
WHILE | Evaluates its expression before each iteration of the #LOOP. If its expression evaluates to false, the #LOOP control sequence terminates. |
FOR | Initializes its counter to the start value, and increments it by the step value each time through the loop. When the counter is greater than the end value, the #LOOP control sequence terminates. |
counter | A user-defined symbol used as the loop counter. |
start | An expression containing the initial value to which to set the loop counter. |
end | An expression containing the ending value of the loop counter. |
BY | Explicitly defines the increment value for the counter. |
step | An expression containing the increment value for the counter. If omitted, the step defaults to one (1). |
TIMES | Loops the number of times specified by the iterations. |
iterations | An expression containing the number of times to loop. |
statements | One or more target and/or Template Language statements. |
#ENDLOOP | Terminates the #LOOP structure. |
A #LOOP structure repetitively executes the statements within its structure. The #LOOP structure must be terminated by #ENDLOOP. If there is no #ENDLOOP, an error message is issued during Template file pre-processing. A #LOOP structure may be nested within another #LOOP structure.
The #LOOP,UNTIL or #LOOP,WHILE statements create exit conditions for the #LOOP. Their expressions are always evaluated at the top of the #LOOP, before the #LOOP is executed. A #LOOP WHILE structure continuously loops as long as the expression is true. A #LOOP UNTIL structure continuously loops as long as the expression is false. The expression may contain Template symbols, constant values, and any of the arithmetic, Boolean, and logical operators documented in the Language Reference. Procedure calls are allowed. If the modulus division operator (%) is used in the expression, it must be followed by at least one blank space (to explicitly differentiate it from the Template symbols).
The #LOOP,FOR statement also creates an exit condition for the #LOOP. The #LOOP initializes the counter to the start value on its first iteration. The #LOOP automatically increments the counter by the step value on each subsequent iteration, then evaluates the counter against the end value. When the counter is greater than the end, the #LOOP control sequence terminates.
#LOOP (without WHILE, UNTIL, or FOR) loops continuously, unless a #BREAK or #RETURN statement is executed. #BREAK terminates the #LOOP and continues execution with the statement following the #LOOP structure. All statements within a #LOOP structure are executed unless a #CYCLE statement is executed. #CYCLE immediately gives control back to the top of the #LOOP for the next iteration, without executing any statements following the #CYCLE in the #LOOP.
Example:
#SET(%LoopBreakFlag,'NO')
#LOOP #!Continuous loop
#INSERT(%SomeRepeatedCodeGroup)
#IF(%LoopBreakFlag = 'YES') #!Check break condition
#BREAK
#ENDIF
#ENDLOOP
#SET(%LoopBreakFlag,'NO')
#LOOP,UNTIL(%LoopBreakFlag = 'YES') #!Loop until condition is true
#INSERT(%SomeRepeatedCodeGroup)
#ENDLOOP
#SET(%LoopBreakFlag,'NO')
#LOOP,WHILE(%LoopBreakFlag = 'NO') #!Loop while condition is true
#INSERT(%SomeRepeatedCodeGroup)
#ENDLOOP