Updated March 17, 2023
Introduction to Loops in VBScript
Looping permits us to recurrently execute a set of statements in code. Loops repeat these statements until a certain condition is met. This condition could either result in true or false or result in running the statement a precise number of times. Some loops are run knowing beforehand the number of iterations the loop is to take, and some are not. Depending on these conditions, we can choose the loop we want to use in our program. For loops are used when we know the number of times we want to repeat our block of code, and Do Loops are used when we do not know how many times we want to iterate, and we have a specific condition that turns to either true and false, based on which we terminate our loop. In this article, we will discuss different types of Loops in VBScript.
Types of Loops in VBScript
We have the following loops in VBScript:
- Do While Loop
- Do Until Loop
- While Wend Loop
- For next Loop
- For-Step-Next Loop
- For-Each-Next Loop
1. Do While Loop
When the number of times we want to iterate through a block of code is not certain, we use Do While Loop. For instance, if we want to print “Hello” on the console, for when the variable value of ‘a’ is less than 6, we use Do While loop.
Code:
<script type="text/vbscript">
Dim a
a=1
Do While a<6
document.write("Hello!")
a=a+1
Loop
</script>
On executing this piece of code, we get:
Hello!Hello!Hello!Hello!Hello!
To understand better, let us look at the working of the code. The code in the loop executes five times as the value of ‘a’ is incremented each time. Control comes out of the loop when the value of ‘a’ becomes 6, and the condition a<6 is no longer satisfied. If we do not write the line a=a+1, our loop becomes an Infinite loop, as there is no exit condition for our loop. Infinite loops can crash our systems. Hence, we need to make sure to have an exit condition in our loop code. We have a variation in the Do While loop, where the loop is always executed at least once. Suppose we assign the variable ‘a’ with the value 6 in the starting itself. Then with the above example, the loop code will not be executed even once. But with the below variation, the loop always executes at least once.
Code:
<script type="text/vbscript">
Dim a
a=6
Do
document.write("Hello!")
a=a+1
Loop While a<5
</script>
With this code, we get the output as ‘Hello!’ printed just once. We check the condition of the Do While Loop after running the loop once.
2. Do Until Loop
Like the Do While Loop, Do Until Loop is also run when we do not know the exact figure of the iterations running on the loop. The difference lies in the syntax and condition.
Code:
<script type="text/vbscript">
Dim a
a=1
Do Until a=6
document.write("Hello!")
a=a+1
Loop
</script>
This code gives us the same result as the first program in Do While loops. We get the result displayed as: Hello!Hello!Hello!Hello!Hello!
We also have a similar alteration in Do Until loop, as we had in Do While loop, where the loop executes at least once. In addition, we have the option of exiting our Do Loops before the execution is done. We can use the Exit Do statement to exit the loop. For instance, if we want to exit the loop when ‘a’ turns to 4, then we can insert an Exit Do statement as follows:
Code:
<script type="text/vbscript">
Dim a
a=1
Do Until a=6
If a=4 Then Exit Do
document.write("Hello!")
a=a+1
Loop
</script>
The output of this code will be:
Hello!Hello!Hello!
3. While Wend Loop
While Wend loop is like the Do While loop, however, it is not used as frequently. This is because the While Wend loop is not as organized as the latter. This is the reason why developers prefer the Do While Loop.
Code:
<script type="text/vbscript">
Dim a
a = 1
While a < 6
document.write("Hello!")
a=a+1
Wend
</script>
On executing this code, we get:
Hello!Hello!Hello!Hello!Hello!
4. For-Next Loop
This loop is used when we know the number of times we want to execute the block of code. In For Loop, we have a counter variable that tells us the number of times the loop is supposed to execute. The Next statement increases the counter variable by one.
Code:
For i = 1 To 5
document.write(" & i & ")
Next
On executing this code, we get:
1
2
3
4
5
5. For-Step-Next Loop
In this loop, the counter variable goes up automatically. We can specify the value by which our counter should increase.
Code:
For i = 1 To 5
document.write(" & i &")
Next
</script>
The output of this code will be:
1
3
5
Like Do Loop, we can exit a For Loop in the middle by using the Exit for statement. For instance:
Code:
<script type="text/vbscript">
For i = 1 To 5 Step 3
If i=4 Then Exit For
document.write(" & i & ")
Next
</script>
The output of this code will be:
1
6. For-Each-Next Loop
When we wish to repeat the loop code for every item in a collection or a VBS array, we use For Each Next loop.
Code:
<script type="text/vbscript">
Dim employees(3)
employees (0)="A"
employees (1)="B"
employees (2)="C"
employees (3)="D"
For Each i In employees
document.write(i)
Next
</script>
On executing the above code, we would get:
A
B
C
D
Loops can be used to run a particular code block over and over again. We use Do Loops and While Loop when the number of times the loop iterates is now known. We use the For Loops when we know the exact times the loop code is to be run.
Recommended Articles
This is a guide to Loops in VBScript. Here we discuss the basic concept, Syntax, types of Loops in VBScript, sample codes, and output. You can also go through our other suggested articles –