VBScript Loops

A loop statement allows the execution of a statement or group of statements multiple times. There are three types of loop statement in VBScript.

  • ‘For’ loop – Useful when it is known in advance the number of times the statements within the loop need to be executed.
  • ‘Do While’ loop – Repeats a statement or group of statements while an expression evaluates to true. There is a condition to get into the loop, so if the condition evaluates to true at the beginning then the statements inside the loop will never execute.
  • ‘Do Until’ loop – This is similar to the ‘Do While’ loop except that instead of executing whilst the condition is true, it executes whilst the condition is false.

‘For’ Loop

The ‘For’ loop takes the following form.

For counter = start To end
    ' Statement(s) to execute.
Next

The ‘counter’ is like a variable, except that it doesn’t need to be defined first. The ‘start’ and ‘end’ values refer to the initial value of the ‘counter’ and the point at which the loop stops, so, for example, ‘start’ and ‘end’ values of 1 and 10 would mean that the statements within the loop would be executed ten times.

The following example displays the value of the ‘counter’ each time through the loop.

For i = 1 To 10
    WScript.Echo i
Next

The resulting output displays the numbers one through ten on a separate line.

1
2
3
4
5
6
7
8
9
10

The ‘step’ keyword can also be included in a ‘For’ loop. This has the effect of increasing the ‘counter’ by the specified value, instead of incrementing by one each time through the loop as in the above example.

For i = 1 To 10 Step 2
    WScript.Echo i
Next

This has the effect of increasing the counter by two with each iteration through the loop, resulting in the following output.

1
3
5
7
9

‘Do While’ Loop

As mentioned above, the ‘Do While’ loop has a condition, which must evaluate to true before the statements within it can be executed.

Do While condition
    ' Statement(s) to execute.
Loop

In the below example a variable, ‘i’, is initialised to 1, then the ‘Do While’ loop checks to see that the variable is less than or equal to 10 before displaying its value. After the value is displayed, the variable is incremented by 1 and the loop starts again.

Dim i
i = 1

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

Note that if the variable ‘i’ had been declared and initialised to eleven or above, then the statements within the loop would not be executed at all. The resulting output is the same as the first ‘For’ loop example above.

‘Do Until’ Loop

The ‘Do Until’ loop is similar to a ‘Do While’ loop except that the statements within the loop continue to execute whilst the condition is false, instead of true.

Do Until condition
    ' Statement(s) to execute.
Loop

Here a variable ‘i’ is declared and initialised to 1, then the ‘Do Until’ loop checks to see if the variable equals 11. If the variable is not equal to 11 then the value is displayed. The value of the variable is then incremented by 1 and the loop starts again. This produces the same results as the ‘Do While’ and first ‘For’ loop examples above.

Dim i
i = 1

Do Until i = 11
    WScript.Echo i
    i = i + 1
Loop

Note that if the variable had been declared and initialised with 11, then the statements within the loop would not be executed at all. If the variable had been initialised with anything above 11, an infinite, or never-ending loop would be created because the variable will never equal 11, so the statements within the loop would continue to execute indefinitely.