Next Statement

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

Syntax
For var1 = initval To finalval [Step Increment ] statements Next var1

Parameters

var1
Specify the name of the counter variable to which repeated data is assigned in the For/Next loop. This variable is normally defined as an integer but may also be defined as a Real variable.
initval
Specify the numeric value to be assigned to the specified variable at the beginning of the loop, either as an expression or directly as a numeric value.
finalval
Specify the numeric value representing the end of the loop, either as an expression or directly as a numeric value. Once this value is met, the For/Next loop is complete and execution continues starting with the statement following the Next instruction.
Step Increment
Specify the value by which the variable is to be incremented for each For/Next loop, either as an expression or directly as a numeric value. This variable may be positive or negative. 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
Describe the SPEL+ statement to be inserted into 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 (initval) is the initial value of the counter. This value may be positive or negative as long as the finalval variable and Step increment correspond correctly.

The second numeric expression (finalval) 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 (var1) is then incremented by the Step value defined by the increment parameter. If the Step option is not used, the counter is incremented by one.

The counter variable (var1) is then compared with the final value (finalval). If the counter is less than or equal to the final value (finalval), the statements following the For instruction are executed again. If the counter variable is greater than the final value (finalval), execution branches outside of the For/Next loop and continues with the instruction immediately following the Next instruction. Nesting of For/Next statements is supported up to 10 levels deep. This means that a For/Next Loop can be put inside of another For/Next loop and so on and so on until there are 10 "nests" of For/Next loops.

Note


  • Negative Step Values

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


See Also
For

Next Statement Example

Function fornext
    Integer ctr
    For ctr = 1 to 10
        Go Pctr
    Next ctr
    '
    For ctr = 10 to 1 Step -1
        Go Pctr
    Next ctr
Fend