VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, 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: "intial_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

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

Loop Statements - "For", "While", and "Do"

"For ... Next" Statements

 "For ... Next" Statement Example Examples

 "While" Statements

 "While" Statement Examples

 "Do ... Loop" Statements

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

"For ... Next" Statements - Updated in 2015, by Dr. Herong Yang