| FOOTING | Index Level | FOR NEXT ... UNTIL |
| Syntax |
FOR variable = num.expression1 TO num.expression2 {STEP num.expression3} {[WHILE | UNTIL] logical.expression}
statements{s} . . NEXT variable |
| Category | BASIC |
| Type | Statement |
| Description |
an iterative, incremental loop statement used to repeat a sequence of statements for a specific number of iterations.
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 ("expression1"), a limiting value ("expression2"), and an optional increment value ("expression3") 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". "for ... next ...until" is a conditional incremental loop statement. It executes while the expression following the "until" clause evaluates to false. The "until" clause must appear on the same line as the "for ... next" statement. General form: for variable = num.exp to num.exp {step num.exp} ... ... until logical.expression statement{s} . . next variable "for ... next ... while" is also a conditional incremental loop statement. It executes while 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 form: for variable = num.exp to num.exp {step num.exp} ... ... while logical.exp statement{s} . . next variable 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" may not be used in the same "for ... next" loop. |
| Options | |
| See Also | statements & functions UNTIL FOR NEXT ... UNTIL LOOP Boolean Evaluation WHILE FOR NEXT ... WHILE NEXT EXIT |
| Example |
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 displays every value in array element 13. 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 This example prints an incrementing counter based on the number of values in array element 13. The loop terminates as soon as a the last value is tested (number.values) or the first null value is found. 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 is displayed or the first null entry is encountered. for i = 1 to 99 while not(array(i) = "") crt array(i) next i |
| Warnings | String math is not supported by the "for" statement. Therefore, the range is limited to 14 digits both before and after the decimal point. |
| Compatibility | D3 7.0 AP R83 |
| FOOTING | Index Level | FOR NEXT ... UNTIL |