| Directives |
|
| Formats |
|
||||||||||||||||||||||||||||||
| 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
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.
yields: 1 2 3 4 5 6 7 8 9 10 The following example strips trailing spaces from the A$:
This uses nested FOR/NEXT loops to generate the string B$ from A$ reversed:
|
||||||||||||||||||||||||||||||
| Format 2 | For Loop, Simplified
Syntax
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.
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.,
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 |
||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
| Format 3 | For Loop, Array
Indexing
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
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:
Example 2; to print all the fields in a record:
Example 3; to process all rows in a GRID
|
||||||||||||||||||||||||||||||
| 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 |
||||||||||||||||||||||||||||||