advr.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
  Summary  Details  Example                Contents  Index  Back
──────────────────────────────────────────────────────────────────────────────
 FOR counter = start TO end [STEP increment]
      [statementblock]
      [EXIT FOR]
      [statementblock]
 NEXT [counter [,counter]...]
 
 Usage Notes
   ■ If you omit the variable in a NEXT statement, NEXT matches the most
     recent FOR...NEXT statement. If a NEXT statement is encountered before
     its corresponding FOR statement, Visual Basic generates the error
     message, "NEXT without FOR."
 
   ■ EXIT FOR can be used only in a FOR...NEXT statement.
 
   ■ When used within nested FOR...NEXT statements, EXIT FOR transfers out
     of the immediately enclosing loop.
 
   ■ 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.
     Visual Basic checks this at run time by comparing the sign of
     (end - start) with the sign of STEP. If both have the same sign, and
     end does not equal start, the FOR...NEXT loop is entered. If not, the
     entire loop is skipped.
 
   ■ Within a 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, the STEP
                       value is added to counter. 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 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 correct form is:
 
         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