Loop through the VBScript (For Each, Do While, Do Until)

There are occasions when you need to perform repetitive tasks or operations again and again or in other words you need to loop through the VBScript code. There are at least 5 constructs that are ideal for achieving the goal and these are –

  • For Each…Next
  • For…Next
  • Do While…Loop
  • Do Until…Loop
  • While…Wend

In this article we will try to understand the syntax and usage of each of the above constructs with an example.

Loop through the VBScript using “For Each…Next”

For Each…Next allows you to walk through a collection of objects and do something with an individual object from the collection. Once it is done, it goes to the next object in the collection. This gives us the ability to iterate through a collection without knowing the number of members in advance.

Syntax:

For Each item In items
   [statement 1]
   [statement 2]
   ....
   [statement m]
   [Exit For]
   [statement m+1]
   [statement m+2]
Next

Example:

Option Explicit
Dim colors, color

'Define an array of colors
colors =  Array("Red","Blue","Green","Yellow")

'Iterate through the array of colors and display each color
For Each color in colors
   WScript.Echo color
next

Loop through the VBScript using “For…Next

This sounds similar to the construct we just discussed. However, the important difference between “For Each…Next” and “For…Next” is that; with “For Each…Next” you don’t need to know how many times you want to do something. With “For…Next” you must know exactly how many times you want to do something.

Syntax:

For counter=start To end [Step step]
  [statements]
  [Exit For]
  [statements]
Next

Example:

Option Explicit
Dim colors,i

'Define an array of colors
colors =  Array("Red","Blue","Green","Yellow")

'Iterate through the array of colors and display each color
For i=0 To 3 Step 1
   WScript.Echo colors(i)
next

Loop through the VBScript using “Do While…Loop

The Do While…Loop command enables you to run a script as long as a certain condition is in effect. You can check the condition before you enter the loop or you can check it after the loop has run at least once.

Syntax:

Do While condition
   [statement 1]
   [statement 2]
   ...
   [statement m]
   [Exit Do]
   [statement 1]
   [statement 2]
   ...
   [statement n]
Loop       

Example:

Check condition first

Option Explicit
Dim i : i =0

Do While i < 5
   i = i + 1
   WScript.Echo i
Loop        

Do something first

Option Explicit
Dim j : j = 0

Do 
 WScript.Echo j
Loop While j > 5 

Loop through the VBScript using “Do Until…Loop

This loop keeps running until a condition becomes false. The condition may be checked at the beginning of the loop or at the end of loop.

Syntax:

Do Until condition
   [statement 1]
   [statement 2]
   ...
   [statement n]
   [Exit Do]
   [statement 1]
   [statement 2]
   ...
   [statement n]
Loop     

Example:

Option Explicit
Dim i 

i = 10
Do Until i>15  'Condition is False.Hence loop will be executed
      i = i + 1
      WScript.Echo i
Loop 

If you are getting confused between “Do While” and “Do Until” then below example should clarify it.

Option Explicit
Dim i 

i = 10
Do Until i>15  'Condition is False.Hence loop will be executed
      i = i + 1
      WScript.Echo i
Loop 

i = 10
Do While i <= 15 'Condition is True. Hence loop will be executed
   i = i+1
   WScript.Echo i
Loop 

Loop through the VBScript using “While…Wend

One more kind of looping available in VBScript is the While…Wend statement. It is read as follows: “While statement A is true, we will continue to loop through the code. Once it is met, then we will exit at the Wend statement.” It is very similar to a Do…Until loop statement.

Syntax:

While condition
 [statements]
Wend

Example:

Option Explicit
i = 0
While i < 5
   i = i+1
   WScript.Echo i
Wend

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *