Part 6: QTP and VBScript | Nested Loops in QTP
In the previous article on using loops in QTP, we saw the basics of different types of loops that you can use in QTP. However in many cases you might have to use nested loops. Also, there are lot of scenarios that require use of loops with arrays and conditional statements. Let’s see how this can be done in QTP.
Using Nested Loops in QTP
Nesting of loops implies a situation where you have a for/do/while loop inside another for/do/while loop (which itself can be inside another loop). One of the most common examples of a nested loop is looping through a multidimensional array. Let’s see an example that illustrates this.
'Declare a 3x3 Array Dim arr(2,2) 'Initialize the array arr(0, 0) = "00" : arr(0, 1) = "01" : arr(0, 2) = "02" arr(1, 0) = "10" : arr(1, 1) = "11" : arr(1, 2) = "12" arr(2, 0) = "20" : arr(2, 1) = "21" : arr(2, 2) = "22" 'Use Nested for Loop to iterate through the array and display each element For iR = 0 to 2 'Looping through Rows For iC = 0 to 2 'Looping through Columns msgbox arr(iR, iC) Next Next
In the above example we used nested for loop of depth 2 to iterate through an array. As shown above, its not necessary that you use the same loop construct while writing nested code. You can use a for loop within a do while loop or any other combination to come up with the nested code. Also you can have any level/depth of nested loops. However, having greater depth of nested loops may cause performance issues and may also result in difficulty/confusions while debugging the code.
Using Conditional Statements with Loops
Let’s see a couple of examples where if and select case conditions are used with loops.
Example 1: Using If condition with For Loop
'Code to find all the numbers between 1 to 5 that are divisible by 5 For i =1 to 50 'Check if the number is divisible by 5 If i mod 5 = 0 Then msgbox "Number " & i & " is divisible by 5" End If Next
Example 2: Using Select Case with For Loop
'Find all the dates that fall on Saturday or Sunday in March 2012 Dim dtDate,sDay dtDate = #03/01/2012# 'Loop through all the days in the month of March For i=1 to 31 'Find the Week Day for the Date sDay = WeekDay(dtDate) 'Check if the Day is Saturday or Sunday Select Case sDay Case 1 : msgbox dtDate & " falls on Sunday" Case 7 : msgbox dtDate & " falls on Saturday" End Select 'Increment Date by One dtDate = dtDate + 1 Next
Using Exit Statement in Loops
Once inside a loop, you can forcefully exit from the loop before all the iterations have been completed. This can be accomplished using Exit Statements. There are two Exit statement that you can use with QTP Looping Constructs. These are –
1) Exit For statement, can be used with For…Next and For Each…Next loops.
2) Exit Do statement, can be used with Do…Loop.
Let’s see an example where the code exits the For…Next and Do…Loop based on some criteria.
'Using Exit statements in loops 'Set up infinite do loop Do For i=1 to 100 'Create a Random Number iRandom = Int(Rnd*10) 'Exit For Loop when Random number is 5 and Do loop when Random number is 9 Select Case iRandom Case 5 : Exit For Case 9 : Exit Do End Select Next Loop msgbox "Loops Exited"
This was all about nested loops and combining loops with conditional statements. Please feel free to provide your feedback for this article and the entire QTP VBScript tutorial series. You can also leave a comment if you would like any other details to be covered here.