FOR Statement
Begins a loop that is terminated by a NEXT statement.
Syntax
FOR variable=initialise TO
test {STEP increment}
{[WHILE || UNTIL] limit-expr}
{statement(s)}
{[WHILE || UNTIL] limit-expr {DO}
{statement(s)}}...
NEXT variable
Syntax Elements
variable is the variable to be incremented or decremented by the NEXT statement.
initialise is the initial value of variable.
test is the limiting value of variable.
increment is the number by which to increment variable. If it is not specified, the default value is 1. increment can be a negative number, causing the loop to count down instead of counting up.
limit-expr is an expression that evaluates to true (1) or false (0). It is similar to the test expression in the IF statement.
Operation
Any statements can appear between a FOR and NEXT statement, including statements that transfer control out of the loop. However, no statement should transfer control into a FOR-NEXT loop, except for the FOR statement itself.
It is not good programming practice to transfer control out of a FOR/NEXT loop with a GOTO statement. Such practice can occasionally result in stacking problems. A better method is to use a WHILE, UNTIL or BREAK statement, or to set variable to the value that causes the NEXT statement to pass control out of the loop.
FOR and NEXT loops can be nested; nested loops are FOR and NEXT loops that are contained inside other FOR and NEXT loops. For example:
FOR I=1 TO 10
FOR J=1 TO 10
PRINT B(I,J)
NEXT J
NEXT I
In this example, the inner loop (FOR J=1 TO 10) executes 10 times for each pass through the outer loop (FOR I=1 TO 10). The outer loop is executed 10 times also, so the statement PRINT B(I,J) is actually executed a total of 100 times. Matrix B is printed in the following order: B(1,1), B(1,2), B(1,3),..., B(1,10), B(2,1), B(2,2),..., B(10,10).
Loops can be nested to any number of levels. However, a nested loop must be completely contained within the range of the outer loop (that is, the ranges of the loops cannot cross).
Loop Control Statements
The following statements can be used within a FOR construct to control loop execution:
BREAK or EXIT Exits the loop immediately and continues from the statement following the appropriate NEXT statement.
CONTINUE Branches to the start of the loop in the same way as a NEXT statement. Note, however, that CONTINUE can be used anywhere in the loop to force the next iteration to start immediately.
Note
The BREAK, EXIT and CONTINUE statements can be used only within loop constructs. If you use any of these outside a loop, your program will not compile.
Compiling FOR/NEXT Statements
The DataBasic Compiler looks for a one-to-one correspondence between each FOR and each NEXT statement. It also ensures that each NEXT statement contains a variable name. However, the compiler does not compare the variable names in the NEXT statements to the variable names in the FOR statements. This necessitates careful programming to avoid improperly nesting of FOR/NEXT loops.
In the two examples below, the list of statements in the left-hand column are properly nested, whereas the statements in the right-hand column are improperly nested. However, both examples will compile without error because the one-to-one correspondence between each FOR and NEXT statement is maintained. In the example on the right, the NEXT I statement increments variable J and the NEXT J statement increments variable I.
FOR I=1 TO 10 FOR J=1 TO 10 PRINT B(I,J) NEXT J NEXT I |
FOR I=1 TO 10 FOR J=1 TO 10 PRINT B(I,J) NEXT I NEXT J |
Examples
FOR I=1 TO 10 STEP .5 UNTIL A>100
.
.
.
NEXT I
Executes until I=10 or until the statements within the loop cause A to be greater than 100.
A=20
FOR J=1 TO 10 WHILE A<25
A += 1
PRINT J, A
NEXT J
Executes five times. Variable A reaches 25 before variable J reaches 10.
A=0
FOR J=1 TO 10 WHILE A < 25
A += 1
PRINT J, A
NEXT J
Executes 10 times. Variable J reaches 10 before variable A reaches 25.
ST="X" FOR B=1 TO 10 UNTIL ST="XXXXX" ST=ST: "X" NEXT B
Executes four times. An X is added to the string variable ST until ST = XXXXX.
FOR X = 1 TO TBLSIZE WHILE TBL1(X) NE "" TBL2(X) = TBL1(X) UNTIL TBL2(X) = 0 NEXT X
Copies TBL1 to TBL2 until a null element is found in TBL1 or a zero element has been moved to TBL2.
FOR X = 1 TO TBLSIZE IF TBL1(X) EQ "" THEN BREAK TBL2(X) = TBL1(X) IF TBL2(X) = 0 THEN BREAK NEXT X
Does the same as the previous example, but uses BREAK statements to exit from the loop.
FOR X = 1 TO LISTSIZE IF LIST1<X> EQ "" THEN CONTINUE LIST2<X> = LIST1<X> NEXT X
Copies LIST1 to LIST2, skipping null elements.