Q(uick)BASIC Statement: FOR...NEXT

Quick View

FOR...NEXT

A control flow statement that repeats a block of statements a specified number of times

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • FOR counter = start TO end [STEP increment][statementblock]NEXT [counter [,counter]…]
Description/Parameter(s)
counter A numeric variable used as the loop counter.
start and end The initial and final values of the counter.
increment The amount the counter is changed each time through the loop.
Example
FOR i% = 1 TO 15 PRINT i% NEXT i% FOR i% = 7 to -6 STEP -3 PRINT i% NEXT i%
Syntax
  • FOR counter = start TO end [STEP increment][statements]NEXT [counter [,counter…]]
Description/Parameter(s)
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 incremented 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. This is checked 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 over.

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 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 continues with the statement following the NEXT statement. (If STEP is negative, the loop repeats until counter is less than end.)

If start and end have the same value, the loop executes once, regardless of the value of STEP. If STEP is zero, the loop repeats indefinitely.

Avoid changing the value of counter within the loop. Changing the loop counter is poor programming practice; it makes 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 the 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 the correct form:

  • 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 EXIT FOR statement.

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, an error message is generated that reads "NEXT without FOR."

Differences from BASICA

  • Unlike BASICA, QuickBASIC 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

This example prints the first 11 columns of Pascal's triangle:

'Print the first MAXCOL 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. CLS ' Clear screen CONST MAXCOL=11 DIM A(MAXCOL,MAXCOL) 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.

Sample Output:

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
Syntax
  • FOR counter = start TO end [STEP increment][statementblock][EXIT FOR][statementblock]NEXT [counter [,counter]…]
Description/Parameter(s)
  • The counter cannot be an array element or a record element.
  • If you do not specify STEP, increment defaults to one.
  • EXIT FOR is an alternative exit from a FOR...NEXT loop. EXIT FOR transfers control to the statement following the NEXT statement. When used within nested FOR...NEXT statements, EXIT FOR transfers out of the immediately enclosing loop. EXIT FOR can be used only in a FOR...NEXT statement.

Usage Notes

  • A FOR...NEXT loop executes only if The arguments 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, the 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 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 the 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 the correct form:

  • 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

Important

  • 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."

Differences from 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

This example uses FOR...NEXT loops to print 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 M FOR M = 3 TO MAXCOL FOR N = 2 TO M - 1 A(M, N) = A(M - 1, N - 1) + A(M - 1, N) NEXT N NEXT M 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 N PRINT Startrow = Startrow - 1 'Next column starts 1 row above NEXT M 'preceding column.