For...Next Statement

The For...Next instructions are used together to create a loop where instructions located between For and Next are executed multiple times as specified by the user.

Syntax
For var = initValue To finalValue [Step increment ] statements Next [var]

Parameters

var
Specify the name of the variable to which repeated data will be assigned. This variable is normally defined as an integer but may also be defined as a Real variable.
initValue
Specifies the number to be assigned to the specified variable at the beginning of the loop.
finalValue
The final value of the counter var. Once this value is met, the For...Next loop is complete and execution continues starting with the statement following the Next instruction.
Step increment
An optional parameter which defines the counting increment for each time the Next statement is executed within the For...Next loop. However, if the value is negative, the initial value of the variable must be larger than the final value of the variable. If the increment value is left out the system automatically increments by “1”.
statements
Any valid SPEL+ statements can be inserted inside the For...Next loop.

Description
For...Next executes a set of statements within a loop a specified number of times. The beginning of the loop is the For statement. The end of the loop is the Next statement. A variable is used to count the number of times the statements inside the loop are executed.

The first numeric expression (initValue ) is the initial value of the counter. This value may be positive or negative as long as the finalValue variable and Step increment correspond correctly.

The second numeric expression (finalValue ) is the final value of the counter. This is the value which once reached causes the For...Next loop to terminate and control of the program is passed on to the next instruction following the Next instruction.

Program statements after the For statement are executed until a Next instruction is reached. The counter variable (var) is then incremented by the Step value defined by the increment parameter. If the Step option is not used, the counter is incremented by “1 (one)”.

The counter variable (var) is then compared with the final value. If the counter is less than or equal to the final value, the statements following the For instruction are executed again. If the counter variable is greater than the final value, execution branches outside of the For...Next loop and continues with the instruction immediately following the Next instruction.

Notes


  • Negative Step Values:

    If the value of the Step increment (increment) is negative, the counter variable (var) is decremented (decreased) each time through the loop and the initial value must be greater than the final value for the loop to work.

  • Variable Following Next is Not Required:

    The variable name following the Next instruction may be omitted. However, for programs that contain nested For...Next loops, it is recommended to include the variable name following the Next instruction to aid in quickly identifying loops.

  • When a variable exits the loop, the value is not a final value.

    Function forsample
      Integer i
      For i = 0 To 3
      Next
      Print i  ' Displays 4
    Fend
    
  • When you exit the loop from the nested structure without using Exit For

    Error 2020 will occur when you repeatedly execute the program which exits the loop by the command other than the Exit For command (such as GoSub statement, Goto statement, and Call statement.) Be sure to use Exit For command to exit the loop.

  • Avoid endless execution of empty Loop Statements and similar to them, use them with the Wait command instead

    Do not use empty For...Next statements and similar commands to avoid effect on the system. The Controllers are detecting endless loop tasks. If the controller determines that the system will be affected, it will stop the program with error 2556 (An excessive loop was detected).

    When performing operations that require a loop or waiting for I/O, execute a Wait command (Wait 0.1) and more within the loop to avoid occupying the CPU.


See Also
Do...Loop

For...Next Statement Example

Function fornext
    Integer counter
    For counter = 1 to 10
        Go Pctr
    Next counter

    For counter = 10 to 1 Step -1
        Go Pctr
    Next counter
Fend