VBScript Tutorials - Herong's Tutorial Examples - Version 6.00, by Dr. Herong Yang
"For ... Next" Statements
This section describes how 'For ... Next' statements work in VBScript. A block of statements is repeated until a loop variable reaches its final value.
"For ... Next" statement is probably the most commonly used loop statement in VBScript. Here is its syntax:
For loop_variable = initial_value To final_value [Step interval] statement_block (multiple statements) Next
where "loop_variable" is a temporary variable used to control the loop, "initial_value" is the initial value to be assigned to the loop variable, "final_value" is the upper bound to stop the loop, and "interval" is the interval that controls how the loop variable to be updated at each iteration. Note that if "interval" is not specified, "1" will be used as the interval.
If you want to know exactly how the "For ... Next" statement will be executed, here is my understanding:
Step 1: "initial_value" is assigned to "loop_variable".
Step 2: Comparing the current value of "loop_variable" against the "final_value".
Step 3: If the current value of "loop_variable" is less than or equal to the "final_value", continue with Step 5.
Step 4: If the current value of "loop_variable" is greater than the "final_value", terminate the loop.
Step 5: Execute "statement_block" enclosed in the "For ... Next" loop.
Step 6: Update the "loop_variable" with the current value plus the "interval"
Step 7: Continue with Step 2.
Note that the above description is based positive "interval". If "interval" is a negative value, the termination condition will be "the loop variable value is less than the final value.
If you want to terminate the loop early, you can use the "Exit For" statement, which will terminate the loop immediately.
Table of Contents