VBScript Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.01

Loop Statements

Part:   1  2  3  

VB Script Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Data Types and Literals

Variables

Logic Operations

String Operations

Conditional Statements

Arrays

Loop Statements

Functions and Subroutines

Built-in Functions

Variable Inspection

... Table of Contents

(Continued from previous part...)

"Do ... Loop" Statements

Personally, I think that two types of loops, "For ... Next" and "While", are enough for any loop programming situations. But the designers of VB decided to give us an extra type of loops, "Do ... Loop" statements, with the following different flavors:

1. "Do While ... Loop" Statements

   Do While condition
      statement_block
   Loop

They are identical logically to:

   While condition
      statement_block
   Wend

2. "Do Until ... Loop" Statements

   Do Until condition
      statement_block
   Loop

They are identical logically to:

   While Not condition
      statement_block
   Wend

3. "Do ... Loop While" Statements

   Do
      statement_block
   Loop While condition

They are identical logically to:

   statement_block
   While condition
      same_statement_block
   Wend

4. "Do ... Loop Until" Statements

   Do
      statement_block
   Loop Until condition

They are identical logically to:

   statement_block
   While Not condition
      same_statement_block
   Wend

I will leave it to you to write some sample scripts for "Do ... Loop" statements as exercises.

Note that VB offers "Exit Do" statements to allow you to terminate "Do ... Loop" statements early.

Conclusions

Question: How many ways of writing a loop in VB? My answer is 7:

  • "For ... Next".
  • "While ... Wend".
  • "Do While ... Loop".
  • "Do Until ... Loop".
  • "Do ... Loop While".
  • "Do ... Loop Until".
  • "For Each ... Next" - Used with arrays and collections. See the array chapter for details.

Part:   1  2  3  

Dr. Herong Yang, updated in 2006
VBScript Tutorials - Herong's Tutorial Notes - Loop Statements