FOR...NEXT Statement ---------------------------------------------------------------------------- Action Repeats a group of instructions a specified number of times. Syntax FOR counter = start TO end -STEP increment- - statementblock- - EXIT FOR- - statementblock- NEXT -counter -, counter-...- Remarks The FOR statement takes the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- counter A numeric variable used as the loop counter. The variable cannot be an array element or a record element. start The initial value of the counter. end The final value of the counter. increment The amount the counter is changed each time through the loop. If you do not specify STEP, increment defaults to one. A FOR... NEXT loop executes only if start and end are consistent with increment. If end is greater than start, increment must be positive. If end is less than start, increment must be negative. BASIC checks this at run time by comparing the sign of ( end - start) with the sign of step. If both have the same sign, the FOR... NEXT loop is entered. If not, the entire loop is skipped. Within the FOR... NEXT loop, the program lines following the FOR statement are executed until the NEXT statement is encountered. Then counter is changed by the amount specified by STEP, and compared with the final value, end. If start and end have the same value, the loop executes once, regardless of the value of STEP. Otherwise, STEP value controls the loop as follows. ----------------------------------------------------------------------------- STEP value Loop execution ---------------------------------------------------------------------------- Positive If counter is less than or equal to end, control returns to the statement after the FOR statement and the process repeats. If counter is greater than end, the loop is exited; execution STEP value Loop execution ---------------------------------------------------------------------------- loop is exited; execution continues with the statement following the NEXT statement. Negative The loop repeats until counter is less than end. Zero The loop repeats indefinitely. Avoid changing the value of counter within the loop. Changing the loop counter is poor programming practice; it can make the program more difficult to read and debug. You can nest FOR... NEXT loops; that is, you can place a FOR... NEXT loop within another FOR... NEXT loop. To ensure that nested loops work properly, give each loop a unique variable name as its counter. The NEXT statement for the inside loop must appear before the NEXT statement for the outside loop. The following construction is correct. FOR I = 1 TO 10 FOR J = 1 TO 10 FOR K = 1 TO 10 . . . NEXT K NEXT J NEXT I A NEXT statement with the form NEXT K, J, I is equivalent to the following sequence of statements. NEXT K NEXT J NEXT I The EXIT FOR statement is a convenient alternative exit from FOR... NEXT loops. See the entry for EXIT. Note If you omit the variable in a NEXT statement, the NEXT statement matches the most recent FOR statement. If a NEXT statement is encountered before its corresponding FOR statement, BASIC generates the error message NEXT without FOR. BASICA Unlike BASICA, BASIC supports double-precision control values ( start, end, and counter) in its FOR...NEXT loops. However, if the control values fall within the range for integers, you should use integer control values for maximum speed. Example The following example prints the first 11 columns of Pascal's Triangle, in which each number is the sum of the number immediately above it and the number immediately below it in the preceding column. DEFINT A-Z CLS ' Clear screen. CONST MAXCOL = 11 DIM A(MAXCOL, MAXCOL) PRINT "Pascal's Triangle" FOR M = 1 TO MAXCOL A(M, 1) = 1. A(M, M) = 1' Top and bottom of each column is 1. NEXT FOR M = 3 TO MAXCOL FOR N = 2 TO M - 1 A(M, N) = A(M - 1, N - 1) + A(M - 1, N) NEXT NEXT Startrow = 13 ' Go to the middle of the screen. FOR M = 1 TO MAXCOL Col = 6 * M Row = Startrow FOR N = 1 TO M LOCATE Row, Col. PRINT A(M, N) Row = Row + 2' Go down 2 rows to print next number. NEXT PRINT Startrow = Startrow - 1' Next column starts 1 row above NEXT' preceding column. Output Pascal's Triangle 1 1 1 10 1 9 1 8 45 1 7 36 1 6 28 120 1 5 21 84 1 4 15 56 210 1 3 10 35 126 1 2 6 20 70 252 1 3 10 35 126 1 4 15 56 210 1 5 21 84 1 6 28 120 1 7 36 1 8 45 1 9 1 10 1 1