FOR and NEXT (execute a set of statements on a condition)
FOR and NEXT perform a series of instructions a given number of times in a loop and cause a variable to be used repeatedly.
Syntax
statement
statement
NEXT V1
where
- V1 is the counter value. The value should be defined using the SET verb.
- V2 is the initial value of the counter. The length attribute of V2 must be the same as the V3 length attribute.
- V3 is the final value of the counter. If a variable name is used, it must have an ampersand (&) prefix.
- statement can be any PRL statement.
The set of statements following the FOR statement are executed until a NEXT statement is encountered and a specific condition is met. There is at least one execution of the series of statements, even if the final counter value is less than the initial value. After each execution, the V1 value is incremented by 1. Processing loops back to the beginning of the set of statements after the FOR statement until the final value is reached. When the counter value is greater than the final value, the statement following the NEXT statement is executed.
Usage
You can
use FOR and NEXT anywhere within the PRL
The FOR-NEXT loop usually follows a SELECT statement.
nest FOR and NEXT loops, as shown in the following example
The counter within each nested loop must have a unique variable name. The NEXT statement for the inner FOR-NEXT loop must appear before the NEXT statement for the outer FOR-NEXT loop.
- use any of the variables that are described in Symbolic-variables or Predefined-variables
Example
FOR &Y = 001 TO 100
SET Z = X + Y
NEXT &Y
NEXT &X