bas7advr.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
  Syntax  Details  Example                 Contents  Index  Back
──────────────────────────────────────────────────────────────────────────────
FOR...NEXT repeats a block of statements a specified number of times.
 
FOR counter = start TO end [STEP increment]
    [statementblock]
[EXIT FOR]
    [statementblock]
NEXT [counter [,counter]...]
 
    ■ 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.