ProvideX V8.20 > Language Reference > 2. Directives
135
Format 2: For Loop, Simplified Syntax
FOR var ..NEXT [var]
This format executes the logic immediately following the FOR by the number of times
specified in the numeric value var; therefore, FOR 1 will execute the loop once, and
FOR 5 will execute the loop 5 times. A value of zero causes the loop to be skipped. An
error is generated if the numeric value is not an integer or it is less than zero.
If var is a simple numeric variable, the system will first set var to 1, then increment
up to its initial value. When var = 5, the loop will execute 5 times, with var starting at
1 and incrementing by 1 through each iteration to 5. At the completion of the loop,
var will equal its initial value. Regardless of whether a simple numeric variable is
used, TCB(19) will contain the current iteration count during the loop.
Example 1:
N = LEN(X$)
FOR N
IF X$(N,1) = "X" THEN X$(N,1) = "Y"
NEXT
Example 2:
TOT = 0
FOR DIM(READ MAX(Balance))
TOT += Balance[TCB(19)]
NEXT
Should a loop using a defined variable be prematurely exited (via BREAK, POP, or
EXITTO), the variable will remain at its last value; e.g.,
N = 10
FOR N
IF X$[N] = "" BREAK
...
NEXT
When the IF condition is satisfied and the FOR loop is exited via the BREAK, the
value in N will be the index into the array.
See Also
BREAK Immediate Exit of Loop, p.33,
CONTINUE Initiates Next Iteration of Loop, p.56
EXITTO End Loop, Transfer Control, p.124
Loop Structures, User's Guide.