FOR (For)
Free-Form Syntax | FOR{(MR)} index-name {= start-value} {BY increment} {TO|DOWNTO limit} |
Code | Factor 1 | Extended Factor 2 | ||||
---|---|---|---|---|---|---|
FOR | index-name = start-value BY increment TO | DOWNTO limit |
The FOR operation begins a group of operations and controls the number of times the group will be processed. To indicate the number of times the group of operations is to be processed, specify an index name, a starting value, an increment value, and a limit value. The optional starting, increment, and limit values can be a free-form expressions. An associated END or ENDFOR statement marks the end of the group. For further information on FOR groups, see Structured Programming Operations.
FOR index-name { = starting-value }
{ BY increment-value }
{ TO | DOWNTO limit-value }
{ loop body }
ENDFOR | END
The starting-value, increment-value, and limit-value can be numeric values or expressions with zero decimal positions. The increment value, if specified, cannot be zero.
The BY and TO (or DOWNTO) clauses can be specified in either order. Both "BY 2 TO 10" and "TO 10 BY 2" are allowed.
In addition to the FOR operation itself, the conditioning indicators on the FOR and ENDFOR (or END) statements control the FOR group. The conditioning indicators on the FOR statement control whether or not the FOR operation begins. These indicators are checked only once, at the beginning of the for loop. The conditioning indicators on the associated END or ENDFOR statement control whether or not the FOR group is repeated another time. These indicators are checked at the end of each loop.
- If the conditioning indicators on the FOR statement line are satisfied, the FOR operation is processed (step 2). If the indicators are not satisfied, control passes to the next operation to be processed following the associated END or ENDFOR statement (step 8).
- If specified, the initial value is assigned to the index name. Otherwise, the index name retains the same value it had before the start of the loop.
- If specified, the limit value is evaluated and compared to the
index name. If no limit value is specified, the loop repeats indefinitely
until it encounters a statement that exits the loop (such as a LEAVE
or GOTO) or that ends the program or procedure (such as a RETURN).
If the TO clause is specified and the index name value is greater than the limit value, control passes to the first statement following the ENDFOR statement. If DOWNTO is specified and the index name is less than the limit value, control passes to the first statement after the ENDFOR.
- The operations in the FOR group are processed.
- If the conditioning indicators on the END or ENDFOR statement are not satisfied, control passes to the statement after the associated END or ENDFOR and the loop ends.
- If the increment value is specified, it is evaluated. Otherwise, it defaults to 1.
- The increment value is either added to (for TO) or subtracted from (for DOWNTO) the index name. Control passes to step 3. (Note that the conditioning indicators on the FOR statement are not tested again (step 1) when control passes to step 3.)
- The statement after the END or ENDFOR statement is processed when the conditioning indicators on the FOR, END, or ENDFOR statements are not satisfied (step 1 or 5), or when the index value is greater than (for TO) or less than (for DOWNTO) the limit value (step 3), or when the index value overflows.
- The index name cannot be declared on the FOR operation. Variables should be declared in the Definition specifications.
- The index-name can be any fully-qualified name, including an indexed array element.
See LEAVE (Leave a Do/For Group) and ITER (Iterate) for information on how those operations affect a FOR operation.
For more information, see Structured Programming Operations.