SoFunction
Updated on 2025-04-08

VBScript Tutorial Lesson 8 Using Loop Statements

Repeat code execution using loop
Loops are used to repeatedly execute a set of statements. Loops can be divided into three categories: one type repeats the statement before the condition becomes False, one type repeats the statement before the condition becomes True, and the other type repeats the statement according to the specified number of times.
The following loop statements can be used in VBScript:

Do...Loop: Loop when (or until) the condition is True.
While...Wend: Loops when the condition is True.
For...Next: Specify the number of loops and repeat the statement using a counter.
For Each...Next: For each item in the set or each element in the array, repeat a set of statements.
Using Do Loop
You can use the Do...Loop statement to run the statement block multiple times (various times). Repeat the statement block when the condition is True or before the condition becomes True.
Repeat the statement when the condition is True
The While keyword is used to check conditions in a Do...Loop statement. There are two ways to check the conditions: check the conditions before entering the loop (such as the ChkFirstWhile example below); or check the conditions after the loop is run at least once (such as the ChkLastWhile example below). During the ChkFirstWhile process, if the initial value of myNum is set to 9 instead of 20, the statement in the loop body is never executed. During the ChkLastWhile process, statements in the loop body are executed only once, because the condition is already False at the time of checking.
Sub ChkFirstWhile()
Dim counter, myNum
counter = 0
myNum = 20
Do While myNum > 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox "Loop repeated" & counter & " times."
End Sub

Sub ChkLastWhile()
Dim counter, myNum
counter = 0
myNum = 9
Do
myNum = myNum - 1
counter = counter + 1
Loop While myNum > 10
MsgBox "Loop repeated" & counter & " times."
End Sub

Repeat the statement until the condition becomes True
The Until keyword is used to check conditions in a Do...Loop statement. There are two ways to check the conditions: check the conditions before entering the loop (such as the ChkFirstUntil example below); or check the conditions after the loop has been run at least once (such as the ChkLastUntil example below). As long as the condition is False, a loop will occur.
Sub ChkFirstUntil()
Dim counter, myNum
counter = 0
myNum = 20
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox "Loop repeated" & counter & " times."
End Sub

Sub ChkLastUntil()
Dim counter, myNum
counter = 0
myNum = 1
Do
myNum = myNum + 1
counter = counter + 1
Loop Until myNum = 10
MsgBox "Loop repeated" & counter & " times."
End Sub

Exit the loop
The Exit Do statement is used to exit the Do...Loop loop. Because usually only exit the loop in certain special cases (for example, to avoid a dead loop), you can use the Exit Do statement in the True statement block of the If...Then...Else statement. If the condition is False, the loop will run as usual.
In the following example, the initial value of myNum will result in a dead loop. If...Then...Else statement checks this condition to prevent a dead loop.

Sub ExitExample()
Dim counter, myNum
counter = 0
myNum = 9
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
If myNum < 10 Then Exit Do
Loop
MsgBox "Loop repeated" & counter & " times."
End Sub

Using While...Wend
While...Wend statements are provided for users who are familiar with their usage. However, since While...Wend lacks flexibility, it is recommended to use the Do...Loop statement.
Use For...Next
The For...Next statement is used to run a statement block for a specified number of times. Use a counter variable in a loop, and the value of this variable increases or decreases with each loop.
For example, the following example repeats the procedure MyProc 50 times. The For statement specifies the counter variable x and its start and end values. The Next statement adds the counter variable by 1 each time.

Sub DoMyProc50Times()
Dim x
For x = 1 To 50
MyProc
Next
End Sub

Keyword Step is used to specify the value of the counter variable to increase or decrease each time. In the following example, the counter variable j is incremented by 2 each time. After the loop is over, the total value is the sum of 2, 4, 6, 8, and 10.
Sub TwosTotal()
Dim j, total
For j = 2 To 10 Step 2
total = total + j
Next
MsgBox "The sum is " & total & "."
End Sub

To decrement the counter variable, set Step to a negative value. At this time, the termination value of the counter variable must be less than the starting value. In the following example, the counter variable myNum is decremented by 2 each time. After the loop is over, the total value is the sum of 16, 14, 12, 10, 8, 6, 4, and 2.

Sub NewTotal()
Dim myNum, total
For myNum = 16 To 2 Step -2
total = total + myNum
Next
MsgBox "The sum is " & total & "."
End Sub

The Exit For statement is used to exit the For...Next statement before the counter reaches its termination value. Because the loop is usually just to exit in some special cases (such as when an error occurs), you can use the Exit For statement in the True statement block of the If...Then...Else statement. If the condition is False, the loop will run as usual.

Use For Each...Next
The For Each...Next loop is similar to the For...Next loop. For Each...Next Instead of running a statement as specified, it repeats a set of statements for each element in the array or for each item in the object collection. This is very useful when you don't know the number of elements in the collection.
In the following example, the contents of the Dictionary object are used to place text in multiple text boxes separately:

<HTML>
<HEAD><TITLE>Forms and Elements</TITLE></HEAD>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub cmdChange_onClick
Dim d 'Create a variable
Set d = createObject("")
"0", "Athens" 'Add keys and items
"1", "Belgrade"
"2", "Cairo"

For Each I in d
(I).Value = (I)
Next
End Sub
-->
</SCRIPT>
<BODY>
<CENTER>
<FORM NAME="frmForm"

<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Button" NAME="cmdChange" VALUE="Click here"><p>
</FORM>
</CENTER>
</BODY>
</HTML>