Directives
FOR..NEXT Loop While Incrementing
   
Formats
1. Conventional Syntax: FOR numvar=first TO last [STEP stepl]
....
NEXT [numvar]
2. Simplified Syntax: FOR numvar
...
NEXT [numvar]
3. Array indexing: FOR numvar INDEX vararry {ALL}
...
NEXT [numvar]
4. Substring parsing: FOR strvar FROM string
...
NEXT [strvar]

Where:

numvar Numeric control variable to be incremented/decremented with each pass through the loop.
first Initial value of the variable var. Numeric expression.
last Ending value of the variable var (value that ends the loop when achieved). Numeric expression.
step Optional value by which the variable will be incremented/decremented with each pass through the loop. The default is 1.
vararray Array whose primary subscript will be used to define the starting/ending value for numvar.
strvar String control variable to receive each substring with each pass through the loop.
string String value (variable or expression) containing a string to parse based a delimiter whose value is defined as the last character of the string.
NEXT Directive to end the loop. Optional var must match current FOR var.
TO Keyword required for Format 1, not case-sensitive.
STEP Optional keyword, not case-sensitive. Sets specific increment/decrement value (default is 1).
Description The FOR directive is used to define the start of a repetitive loop of instructions in a program. The end of the loop is defined by a subsequent NEXT directive following in the program code. (If you specify a variable with a NEXT directive, it must match the variable in the FOR directive.)

The NEXT directive can appear anywhere in the program except where it would be executed inside another FOR/NEXT loop, a GOSUB/RETURN routine, a WHILE/WEND loop, or a REPEAT/UNTIL loop. The control variable can be omitted from the NEXT directive because the increment/decrement of the FOR var is assumed automatically. However, the NEXT var is useful for readability purposes, especially if it appears within a nested loop structure.

Use the EXITTO directive to halt a FOR/NEXT loop without performing all iterations. When ProvideX executes a EXITTO directive, it removes the top entry from the FOR/NEXT stack and ends that FOR/NEXT loop.



*Note* The test to determine if the loop will be terminated occurs when the NEXT directive is encountered; therefore, if the initial value of the loop control variable exceeds the ending value, the loop will execute once.


Format 1 For Loop, Convential Syntax

FOR numvar=first TO last [STEP step]
...
NEXT
[numvar]

When using conventional syntax, the keyword TO is required in order to define first and last values in the loop.

The STEP value, if you use one, is evaluated and saved as the increment (or decrement if negative). If you do not include an increment/decrement, the default is 1 (one). An increment of 0 zero is invalid and results in an Error #44: Invalid step value.

The current program statement position is saved and execution continues. When ProvideX encounters NEXT with the same variable as in the FOR directive (or no variable name) it increments/decrements the value. If the new value does not exceed the end value (or fall below it, if decremented), control transfers back to the FOR directive to continue the loop. Otherwise the FOR/NEXT loop ends.

Examples:

0010 FOR I = 1 TO 10
0020 PRINT I,
0030 NEXT I

yields: 1 2 3 4 5 6 7 8 9 10

The following example strips trailing spaces from the A$:

0030 A$=......
0040 GOSUB 1000
......
1000 IF A$="" THEN I=0; RETURN
1010 FOR I=LEN(A$) TO 1 STEP -1
1020 IF A$(I,1)<>" " THEN EXITTO 1040
1030 NEXT
1040 RETURN

This uses nested FOR/NEXT loops to generate the string B$ from A$ reversed:

0010 INPUT "Enter character string:",A$
0020 IF A$="" THEN STOP
0030 B$=A$
0040 FOR I=1 TO LEN(A$)
0050 FOR J=LEN(A$) TO 1 STEP -1
0060 B$(I,1)=A$(J,1)
0070 NEXT J; NEXT I
0080 PRINT "The answer is: ", B$
0090 GOTO 0010

   
Format 2 For Loop, Simplified Syntax

FOR numvar
...
NEXT
[numvar]

This format executes the logic immediately following the FOR by the number of times specified in the numeric value numvar; 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 numvar is a simple numeric variable, the system will first set numvar to 1, then increment up to its initial value. When numvar = 5, the loop will execute 5 times, with numar starting at 1 and incrementing by 1 through each iteration to 5. At the completion of the loop, numvar 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 Nwill be the index into the array

   
 
FOR Array indexing and sub-string parsing are +PxPlus Exclusives
(build 9181)
Format 3 For Loop, Array Indexing

FOR numvar INDEX vararray {ALL}
...
NEXT
[numvar]

This format executes the logic immediately following the FOR by the number of times determined by the primary index of the array specified by vararry. Only the primary subscript dimension is used.

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.

The system will initially set numvar to lowest index value for vararray, then increment by one (1) up to the highest index value.

   
Format 4 For Loop, Substring parsing

FOR strvar FROM string
...
NEXT
[strvar]

This format executes the logic immediately following the FOR based on the number of substrings found within the value of string. The substrings are defined using the last character of string as a substring delimiter. As each substring is extracted from string, it will be placed into strvar. If string is a null string, the FOR loop is skipped.

Should the loop prematurely exited (via BREAK, POP, or EXITTO), the variable will remain at its last value, however if the loop terminates due to all substrings being processed, the variable will be set to null.

Example 1:

FOR X$ FROM "Canada,USA,France,UK,Germany,Australia,"
IF C$ = X$ THEN BREAK
NEXT
IF X$="" PRINT "Unknown country"

Example 2; to print all the fields in a record:

READ RECORD (file) R$
FOR FLD$ FROM R$
PRINT FLD$
NEXT FLD$

Example 3; to process all rows in a GRID

GRID FIND Grid_ctl,0,0,GridData$
FOR GridRow$ FROM GridData$
....
NEXT

   
   
See Also BREAK Immediate Exit of Loop,
CONTINUE Initiates Next Iteration of Loop
EXITTO End Loop, Transfer Control.
Structured Error handling using ON ERR - an +PxPlus Exclusive