Statement FOR Foundation
Iteratively executes a program loop up to a maximum count.
FOR <CounterVar> := <nStart> TO <nEnd> [STEP <nStep>]
<SourceCode>...
[EXIT]
<SourceCode>...
[LOOP]
<SourceCode>...
NEXT
The FOR..NEXT control structure defines a block of program statements which is continuously executed until the value of the variable <CounterVar> has exceeded the value of the expression <nEnd>. When the program hits the NEXT statement, it begins again with the FOR statement and evaluates all expressions in this line. That means that within a FOR...NEXT loop, the value of the variable <CounterVar> and/or the value of the expressions <nEnd> and <nStep> can be changed. <nStart> cannot be changed since this value is assigned to the counter variable at the initial entry into the loop. If a LOOP statement is executed within the FOR..NEXT body, the loop begins again without executing source code between LOOP and NEXT. By using the EXIT statement, the loop can be immediately exited without <CounterVar> changing again. In this case, the program continues at the first executable program line following NEXT.
The beginning value <nStart> can be greater than the end value <nEnd>. In this case, STEP must be defined with a negative value and the variable <CounterVar> is decremented until it reaches the value of <nEnd>.
FOR...NEXT loops can be nested and can contain other control structures except BEGIN SEQUENCE in connection with the RECOVER statement. Within the BEGIN SEQUENCE..RECOVER control structure, no LOOP or EXIT statement is permitted.
// The example shows a typical use for FOR...NEXT loops: stepping
// through an array. The first loop fills the array in ascending order;
// the second loop steps through the array in descending order,
// displaying every other element.
PROCEDURE Main
LOCAL aArray[100]
FOR n := 1 TO 100 // logarithm, squared number, square root
aArray[n] := { Log(n), n**2, Sqrt(n) }
NEXT
FOR n := 100 TO 1 STEP -2 // Output only for even numbers
? n, aArray[n,1], aArray[n,2], aArray[n,3]
NEXT
RETURN
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.