Commands and Statements

Statement FOR Foundation

Iteratively executes a program loop up to a maximum count.

Syntax
FOR <CounterVar> := <nStart> TO <nEnd> [STEP <nStep>]
    <SourceCode>...
    [EXIT]
    <SourceCode>...
    [LOOP]
    <SourceCode>...
NEXT
Parameters
<CounterVar>
<CounterVar> designates the variable in which the loop counter is stored and updated after each pass through the FOR...NEXT loop. If <CounterVar> does not exist, it is generated as a PRIVATE variable.
<nStart>
<nStart> is a numeric value which is assigned to the variable <CounterVar> at the beginning of the loop. When <nStep> is a negative value, the value of <nEnd> must be less than the value of <nStart>.
<nEnd>
<nEnd> is a numeric value which determines the end of the loop. As soon as <CounterVar> reaches the value of <nEnd>, the loop is exited. When <nStep> is a negative value, the value of <nEnd>must be less than the value of <nStart>.
<nStep>
The optional STEP numeric value <nStep> can be defined so that the loop counter <CounterVar> is changed by this value after each loop. The default value is 1. When <nStep> has a negative value, <CounterVar> is reduced and the value of <nEnd> must be less than the value of <nStart>.
EXIT
The EXIT statement terminates the FOR...NEXT loop and branches the program to the program line following the NEXT statement.
LOOP
The LOOP statement branches the program to the beginning of the FOR...NEXT loop.
Description

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.

Examples
FOR...NEXT usage
// 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 
Feedback

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.