qb45advr.hlp (Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
FOR...NEXT Statement Details
  QuickSCREEN      Details      Example      Contents      Index
──────────────────────────────────────────────────────────────────────────────
FOR...NEXT Statement Details
 
Syntax
  FOR counter = start TO end [STEP increment]
    [statements]
  NEXT [counter [,counter...]]
 
  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."the value of
        attribute is 1.
 
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.