for...next statement
.mdThe for...next statement is an iterative, incremental loop statement used to repeat a sequence of statements for a specific number of occurrences.
Syntax
Description
A for...next loop executes a set of statements for successive values of a variable until a limiting value is encountered. Such values are specified by establishing an initial value (num.exp1), a limiting value (num.exp2), and an optional increment value (num.exp3) to be added at the end of each pass through the loop.
If the loop-ending condition is not met, the loop variable is incremented by 1 or by the value of the step expression and program control transfers back to the beginning of the loop. This looping continues until the ending condition is met or the loop is explicitly exited with an exit or goto statement.
for...next...until
for...next...until is a conditional incremental loop statement. It executes when the expression following the until clause evaluates to false. The until clause must appear on the same line as the for...next statement.
General Formfor var= num.expto num.exp{step num.exp}... ...until logical.exp statement{s} . . next var
for...next...while
for...next...while is also a conditional incremental loop statement. It executes when the expression following the while evaluates to true. Like the until clause, the while statement must appear on the same line as the for ... next statement.
General Formfor var= num.expto num.exp{step num.exp}... ...while logical.exp statement{s} . . next var
The expression in the optional while or until clause is a logical expression. If the while expression is true, the for...next loop continues. If the expression is false, program control passes to the statement immediately following the accompanying next statement.
If the until expression is true, program control passes to the statement immediately following the accompanying next statement. If the expression is false, the for...next loop continues. When the loop is exited, the loop variable retains its last value. The variable in the next statement must be the same as the variable in the for statement.
until and while cannot be used in the same for...next loop.
Example(s)
This example displays every value in array element 13.
number.values = dcount(array(13),char(253)) for i = 1 to number.values crt i "l#4" : array(13)<1,i> next i
This example prints an incrementing counter based on the
number of values in array element 13. The loop terminates as soon
as the last value is tested (number.values) or the
first null value is found.
for i = 100 to 1 step -1 crt i "l#4" : next i This is a decrementing counter beginning at 100 and ending at 1. number.values = dcount(array(13),char(253)) for i = 1 to number.values until array(13)<1,i>=’’ print i next i
All of the above for...next constructs can be constructed using the loop...repeat constructs.
for i = 1 to 99 until array(i) = "" crt array(i) next i
There are 99 elements in the dimensioned array array. This example prints all elements until a null value
is found.
t=time() timeout=10 for try = 1 to 5 until ((time() - t)>timeout or system(14)) sleep 2 next try
This for...next loop has a maximum of 5 iterations. The loop terminates when the elapsed time in seconds exceeds 10, or there are characters in the type-ahead buffer.
for i = 1 to 99 while array(i) # "" crt array(i) next i
This loop terminates when the 99th entry displays or the first null entry is encountered.
for i = 1 to 99 while not(array(i) = "") crt array(i) next i