Introduction to Loops in VBScript: VBScript Tutorial #5
In my previous tutorial in this VBScript tutorial series, we learned about ‘Conditional Statements in the VBScript‘. In this tutorial, I will discuss the different looping structures that are used in the VBScript.
Loop is an important topic in VBScript, hence you should have a good understanding of loops for better programming experiences and to proceed further with the subsequent topics in an easy manner.
This tutorial gives you a complete overview of the meaning of Loops and its different types along with clear examples for your easy understanding.
Table of Contents:
What are Loops?
Generally, Loop means to repeat something several times. In the same way, Loops in the VBScript means those statements in the code which can be repeated several times until any particular condition reaches to an end.
A sequence is followed while using a loop and the statement which comes at the beginning of the code is executed first and so on. Whenever repetitions of some particular statements are required in the code then loops are used until the condition is fulfilled.
Let me take a simple example to explain the concept easily.
Example:
If you want to send an invitation to 10 people with the same message then you can use ‘for loop’ in this case as a counter is fixed and you know the message which is to be repeated 10 times.
The syntax of the loop will be as follows:
For i = 1 to 10
Msgbox “Please do come to my party”
Next
Let’s move to the different types of loops that are supported by VBScript.
Different types of Loops in the VBScript
There are several types of Loops in the VBScript that can be used under various scenarios based on the requirements of a code.
Broadly, there are 3 types of loops in the VBScript, which are as follows:
- For Loop
- Do Loop
- While Loop
These types further include some other loops as well. Let’s discuss them one by one.
#1) For Loop
This is the most basic and widely used loop. This is used in those scenarios where you know the fixed number of times for executing the statements in a code i.e. there is some fixed number of times/fixed iteration count to perform a condition.
Also read =>> For Loop in Java
Example to show the usage of ‘For Loop’ is as follows:
<html> <head> <title>Let’s see implementation of For Loop</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val For val = 1 to 4 Msgbox “Hello All. I am Number:” & val & “<br />” Next </script> </body> </html>
The output of this is:
Hello All. I am Number:1
Hello All. I am Number:2
Hello All. I am Number:3
Hello All. I am Number:4
Let’s understand the working of the code:
- ‘For Loop’ is starting with a counter value (which we are defining with the variable name ‘var’) of 1 and this will repeat 4 times as the counter is from 1 to 4.
- The statement inside the loop is executed adjoining with the value of the variable.
- The counter will be increased by 1 using the ‘Next’ keyword.
- Again the same process will go on and this will last for 4 times as the range is from 1 to 4.
For Each Loop
Each Loop is an extension of For Loop. This is used in case of ‘Arrays’. When you want to repeat the code for each index value of an array then you can use ‘For Each Loop’. This works in the same manner as the above but the implementation is slightly different.
Let’s see its usage with the help of a Simple Example:
<html> <head> <title>Let’s see implementation of For Each Loop</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim array(3) array(0) = 10 array(1) = 20 array(2) = 30 array(3) = 40 For Each val in array Msgbox “Hello All. I am Number:” & val & “<br />” Next </script> </body> </html>
The output of this is:
Hello All. I am Number:10
Hello All. I am Number:20
Hello All. I am Number:30
Hello All. I am Number:40
Let’s understand the working of the code:
- An array is defined by the name ‘array’ with the index values ranging from 0 to 3.
- ‘For each loop’ will start from 0 indexes of an array and go on till it reaches to 3 i.e. loop will go 4 times.
- Code written inside the loop will be executed 4 times with value of ‘val’ variable changing as per the index values of an array.
- When all the index values are executed, the loop will come to an end and the cursor will move to the next statement of the loop.
For Loop with ‘Step’ keyword and ‘Exit For’ Statement
In the case of ‘For Loop’, the counter is incremented by 1 when it comes to ‘Next’ keyword. But if you wish to change this value and if you want to specify the counter value by yourself then you can do so with the help of ‘Step’ keyword. It can be a positive or negative value depending on the requirement and accordingly it will increase or decrease the counter value.
Let’s understand the usage of Step Keyword with the help of a Simple Example:
<html> <head> <title>Let’s see implementation of For Loop with Step keyword</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val For val = 1 to 4 Step 2 Msgbox “Hello All. I am Number:” & val & “<br />” Next </script> </body> </html>
The output of this is:
Hello All. I am Number:1
Hello All. I am Number:3
Let’s see the usage of ‘Exit For’ Statement by taking the reference from the above Example:
<html> <head> <title>Let’s see usage of For Loop with Step keyword and Exit For</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val For val = 1 to 4 Step 2 Msgbox “Hello All. I am Number:” & val & “<br />” If val = 3 Then Exit For End If Next </script> </body> </html>
The output of this is:
Hello All. I am Number:1
‘Exit For’ is used to exit from the ‘For Loop’ block of the code. If anytime, in between the loop you wish to exit, then you can do so using the ‘Exit For’ Statement. In the above example, ‘For Loop’ is terminated when a value is equal to 3 and hence, the message is displayed only once.
Let’s take a look at the next type of loop.
#2) Do Loop
Do Loops are used when you are not sure about the number of repetitions (unlike in case of For Loop) that can take place in the code on the basis of some conditions.
There are 2 types of Do Loops in the VBScript.
They are:
- Do While Loop
- Do Until Loop
Let’s discuss each of them in detail.
Do While Loop
This makes use of keywords ‘Do’ and ‘While’. This can further be divided into 2 cases depending upon the placement of the ‘Do’ and ‘While’ keywords. In the first case, Do and While are used in the beginning of the loop and in other cases, Do is used in the beginning of the Loop whereas While is used at the end of the loop.
Let’s see the implementation of both with the help of some Simple Examples:
Case 1: Do While….Loop
<html> <head> <title>Let’s see usage of Do While Loop with Exit Do Statement</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val val = 1 Do While val <= 6 Msgbox “This is value “& val If val = 4 Then Exit Do End If val = val * 2 Loop </script> </body> </html>
The output of this is:
This is value 1
This is value 2
This is value 4
Let’s understand the working of the code:
- Value of a variable (val) is declared and defined explicitly outside the loop unlike in the case of For Loop where it is declared in the For Loop statement only.
- Do while Loop starts with the checking of the condition if the value of a variable is less than or equal to 6.
- The message written inside the loop displays when the condition gets satisfied.
- If the value of a variable is equal to 4 then the loop is terminated as Exit Do statement is used at this point and the cursor will move to the next statement of Do While Loop. Hence no output is produced after the value of the variable becomes equal to 4.
- The counter is then incremented on the basis of the increment condition that is assigned i.e. val * 2 unlike in the case of ‘For Loop’ where the counter is automatically incremented by 1 with the use of ‘Next’ keyword.
Note: If the value of a variable is declared as 10 i.e. val = 10 in the above example then Do While Loop cannot be executed even at once as condition val <=6 can never become true.
Case 2: Do….Loop While
As I mentioned in above note that Do While maybe not able to execute even at once when the condition is not satisfied at all. Do….While solves this issue and in this case even if the condition is not satisfied but at least one-time loop can be executed.
Let’s understand this concept by taking the reference from the above Example:
<html> <head> <title>Let’s see usage of Do….While Loop </title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val val = 10 Do Msgbox “This is value “& val val = val * 2 Loop While val <= 6 </script> </body> </html>
The output of this is:
This is a value of 10
Let’s understand the working of the code:
- Value of a variable (val) is declared and defined explicitly outside the loop i.e. val = 10.
- Do Loop starts without the checking of the condition (value of a variable is less than or equal to 6) and the Message written inside the loop will be executed i.e. loop will execute at least once.
- The counter is then incremented on the basis of the increment condition that is assigned i.e. val * 2 i.e. 10 * 2 = 20.
- Finally, the condition is checked at the end of the loop which will fail as val = 10 which is not less than 6. Hence, Do While Loop will get terminated here.
Do Until Loop
This works in the same manner as ‘Do While’ Loops but with a difference that the Do While loop initially checks the condition and if it is true only after that the statements are executed and in the case of Do Until, the loop will be executed until the condition becomes false. This is used when you are not sure about the number of times when the loop can be executed.
Do Until Loop is also divided into 2 cases like in the case of Do While.
Let’s take a look at their usage with the help of simple Examples:
Case 1: Do Until….Loop
<html> <head> <title>Let’s see usage of Do Until Loop</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val val = 1 Do Until val = 6 Msgbox “This is value “& val val = val + 1 Loop </script> </body> </html>
The output of this is:
This is value 1
This is value 2
This is value 3
This is value 4
This is value 5
Let’s understand the working of the code:
- Value of a variable (val) is declared and defined explicitly outside the loop i.e. val = 1.
- ‘Do Until’ Loop starts with the checking of the condition that the value of a variable should not be equal to 6.
- The message written inside the loop displays when the condition is satisfied.
- Counter is then incremented on the basis of the increment condition that is assigned i.e. here it is incrementing by 1 i.e. val = val + 1
- Loop will work till val = 5 as when the val becomes 6 then the condition becomes false and the loop will come to an end.
Note: If the value of a variable is declared as 6 (val = 6) in the above example then ‘Do Until’ Loop cannot be executed even at once as when val =6, the condition becomes false and a loop cannot be executed at all.
Case 2: Do….Loop Until
As mentioned in the above note that ‘Do Until’ loop may not be able to execute even at once when the condition is not satisfied at all; Do….Until solves this issue and in this case even if the condition is not satisfied, at least one-time loop can be executed.
Let’s understand this concept by taking the reference from the above Example:
<html> <head> <title>Let’s see usage of Do….Until Loop </title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val val = 5 Do Msgbox “This is value “& val val = val + 1 Loop Until val = 6 </script> </body> </html>
The output of this is:
This is value 5
Let’s understand the working of the code:
- Value of a variable (val) is declared and defined explicitly outside the loop i.e. val = 6.
- ‘Do’ Loop starts without checking the condition if the value of a variable is less than 6 and the Message written inside the loop will be executed i.e. loop will execute at least once.
- Counter is then incremented on the basis of the increment condition that is assigned i.e. val + 1 i.e. 6 + 1 = 7.
- Finally, the condition is checked at the end of the loop which will fail as the val is equal to 6 and hence ‘Do Until’ Loop will be terminated.
#3) While Loop
However, this is same as the ‘Do While’ loop which we discussed just now but as it’s good to know about all the types of loops, let’s see about this too. This is also used when you are not sure about the number of repetitions in a loop. This tests the condition before entering the loop.
Let’s understand this loop with the help of a Simple Example:
<html> <head> <title>Let’s see usage of While Loop</title> </head> <body> <script language=”vbscript” type=”text/vbscript”> Dim val val = 1 While val <= 6 Msgbox “This is value “& val val = val * 2 Wend </script> </body> </html>
The output of this is:
This is value 1
This is value 2
This is value 4
Let’s understand the working of the code:
- Value of a variable (val) is declared and defined explicitly outside the loop i.e. val = 1.
- ‘While’ Loop starts with checking of the condition if the value of a variable is less than or equal to 6
- Message written inside the loop displays when the condition is satisfied
- The counter is then incremented on the basis of the increment condition that is assigned i.e. val will be multiplied by 2 every time when the condition satisfies.
- When the value of a variable becomes more than 6, the loop will come to an end and the statements written after ‘Wend’ keyword will be executed.
Conclusion
I hope that you must have gained good knowledge about the meaning and different types of loops in the VBScript through this tutorial. This, in turn, will help you in proceeding with the upcoming tutorials of the series.
Next Tutorial #6: We will discuss ‘Procedures and Functions’ in the VBScript in my next tutorial.
Stay tuned and share your experience working with Loops and let us know your thoughts about this tutorial.