By Anish Pillai Anish Pillai Posted under qtp and vbscript tutorials | QTP Tutorials for Beginners

Part 6: QTP and VBScript | Nested Loops in QTP

1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×

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)

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


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


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

  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



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.

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.

1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×
  • Keerti

    Hi Anish!

    I actually am struck thinking what made you take that random number…Does Rnd stand for random number?Why did you multiply it by 10?Can you please explain me what exactly is happening in that code?

    • Anish10110

      Hi Keerti,

      Rnd is a function that will return a random number greater than 0 and less than 1. And this value can be anything between this range. Like 0.1, 0.2, 0.01, 0.0002, 0.00000000004, 0.99999999999 etc.

      Now if you notice all the values are less than 1. And If you find Int(<number less than 1>) the result will always be 0. So I multiplied the result with 10. So that if the number is 0.1, 0.2, 0.3 etc.. multiplying it with 10 will give you a number greater than 0.

  • venkat

    If we are enter any year in the calendar year .

    If every month has 14 th day of Wednesday .

    In one year how many months in mentioned day.

    how write the program in QTP.

    can anyone please help

1 Flares Twitter 0 Facebook 0 Google+ 1 LinkedIn 0 Email -- 1 Flares ×