Part 5: QTP and VBScript | Looping through Code
In this fifth installment of QTP VBScript Tutorial series, we would cover the various looping statements that are available in VBScript. Looping statements allow you to repeatedly execute a set of statements multiple times. The number of times the code is executed depends upon many factors such as some loops are executed until a condition is True or False while some other loops are executed a fixed number of times. VBScript provides you with the following four looping statements –
- .. For Loop
- .. Do While Loop
- .. Do Until Loop
- .. For Each Loop
Let’s see each of these four loops one by one.
For Loop
For Loop uses a counter to run a group of statements a specified number of times. Lets see an example of for loop where the codes finds out the sum of numbers from 1 to 10.
Dim iSum iSum = 0 'Loop through the numbers For i=1 to 10 iSum = iSum + i Next 'Display the result msgbox "Result is: " & iSum
In the above example you would have noticed that the counter, i, is incremented by a value 1. If you wish you can change the default value to any other value. This can be done using the Step property. Let’s see an example where the code finds out the sum of first 10 odd numbers.
Dim iSum iSum = 0 'Loop through the numbers For i=1 to 20 Step 2 iSum = iSum + i Next 'Display the result msgbox "Result is: " & iSum
Do While Loop
Do While Loop runs a set of statements while a condition is True. This means that the loop will continue execution till the condition is true and would stop only when the condition becomes false. In a Do While loop the condition can be evaluated at two places. These are –
a) before you enter the loop.
b) at the end of the loop after it has run once. In this case, the loop is executed at least once even if the condition is false because the condition is checked at the end of the loop after it is run once.
Let’s see examples of both these types of Do While Loop.
a) Evaluation of condition before entering the loop.
'Code to find the sum of first 10 numbers Dim iSum, iCounter iSum = 0 : iCounter = 1 'Check the condition at the beginning of the loop Do While iCounter <= 10 iSum = iSum + iCounter 'Increment the counter iCounter = iCounter + 1 Loop 'Display the result msgbox "Result is: " & iSum
b) Evaluation of condition at the end of the loop after it is run once.
'Code to find the sum of first 10 numbers Dim iSum, iCounter iSum = 0 : iCounter = 1 'Check the condition at the end of the loop Do iSum = iSum + iCounter 'Increment the counter iCounter = iCounter + 1 Loop While iCounter <= 10 'Display the result msgbox "Result is: " & iSum
Do Until Loop
Do Until loop is similar to Do While loop with the only difference being that the loop is run until the condition becomes True. In other words, the loop will be executed till the condition is false and would exit only when the condition becomes true. In Do Until Loop also you can check the condition before the beginning or at the end of the loop. Lets take the examples used for Do While loop and see how it can be used for Do Until loop.
a) Evaluation of condition before entering the loop.
'Code to find the sum of first 10 numbers Dim iSum, iCounter iSum = 0 : iCounter = 1 'Check the condition at the beginning of the loop Do Until iCounter > 10 iSum = iSum + iCounter 'Increment the counter iCounter = iCounter + 1 Loop 'Display the result msgbox "Result is: " & iSum
b) Evaluation of condition at the end of the loop after it is run once.
'Code to find the sum of first 10 numbers Dim iSum, iCounter iSum = 0 : iCounter = 1 'Check the condition at the end of the loop Do iSum = iSum + iCounter 'Increment the counter iCounter = iCounter + 1 Loop Until iCounter > 10 'Display the result msgbox "Result is: " & iSum
In the above examples you would have noticed that the when we used the Until statement, we changed the condition to ‘iCounter > 10’. So here the loop will run till the value of iCounter is less than or equal to 10. The loop would exit only when the value becomes greater than 10.
For Each Loop
For Each loop is different from other loops discussed above for the reason that For Each loop repeats a set of statements for each item in a collection/object or for each element in an array. Let’s see some examples of For Each Loop.
Example 1: To find all elements in an array using For Each Loop
'Declare and initialize an array Dim arr(2) arr(0) = "automation" arr(1) = "repository" arr(2) = "qtp" 'Loop through the elements of the array For Each oArr in arr 'Display the array element msgbox oArr Next 'How the same information can be extracted using For Loop iTotalItems = UBound(arr) For i=0 to iTotalItems 'Display the array element msgbox arr(i) Next
Example 2: Find all the files in a given folder using For Each Loop
'Get instance of all the files in a given folder Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder("D:\") Set oFiles = oFolder.Files 'Loop through all the files in 'D:\' Drive For Each file in oFiles 'Display the name of the file msgbox file.Name Next
This was all about the basics of the different looping statements that you can use in VBScript. In the next article, we would cover come more aspects of VBScript looping statements.
If you enjoyed this article, you can join our blog to get new articles delivered directly in your inbox.
To check out more tutorials, visit our QTP Tutorials page. You can also check the Archives page to view the list of all our articles.
Pingback: Part 6: QTP and VBScript | Nested Loops in QTP - Automation Repository - Automation Repository()