{"id":1271,"date":"2012-03-01T20:15:13","date_gmt":"2012-03-01T14:45:13","guid":{"rendered":"http:\/\/www.automationrepository.com\/?p=1271"},"modified":"2012-03-01T20:18:18","modified_gmt":"2012-03-01T14:48:18","slug":"qtp-and-vbscript-nested-loops-in-qtp","status":"publish","type":"post","link":"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/","title":{"rendered":"Part 6: QTP and VBScript | Nested Loops in QTP"},"content":{"rendered":"<div style=\"text-align: justify; font-family: Verdana;\">\n<p>In the previous article on <a title=\"Part 5: QTP and VBScript | Looping through Code\" href=\"http:\/\/www.automationrepository.com\/2012\/02\/qtp-and-vbscript-tutorials-looping-through-code\/\" target=\"_blank\">using loops in QTP<\/a>, 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&#8217;s see how this<!--more--> can be done in QTP.<br \/>\n<span><br \/>\n<\/span><\/p>\n<h3>Using Nested Loops in QTP<\/h3>\n<p>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&#8217;s see an example that illustrates this.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Declare a 3x3 Array\r\nDim arr(2,2)\r\n\r\n'Initialize the array\r\narr(0, 0) = &quot;00&quot; : arr(0, 1) = &quot;01&quot; : arr(0, 2) = &quot;02&quot;\r\narr(1, 0) = &quot;10&quot; : arr(1, 1) = &quot;11&quot; : arr(1, 2) = &quot;12&quot;\r\narr(2, 0) = &quot;20&quot; : arr(2, 1) = &quot;21&quot; : arr(2, 2) = &quot;22&quot;\r\n\r\n'Use Nested for Loop to iterate through the array and display each element\r\nFor iR = 0 to 2 'Looping through Rows\r\n  For iC = 0 to 2 'Looping through Columns\r\n    msgbox arr(iR, iC)\r\n  Next\r\nNext\r\n<\/pre>\n<p><em>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<\/em>.<br \/>\n<span><br \/>\n<\/span><\/p>\n<h3>Using Conditional Statements with Loops<\/h3>\n<p>Let&#8217;s see a couple of examples where if and select case conditions are used with loops.<br \/>\n<em><\/em><br \/>\n<strong>Example 1: Using If condition with For Loop<\/strong><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Code to find all the numbers between 1 to 5 that are divisible by 5\r\n\r\nFor i =1 to 50\r\n\r\n  'Check if the number is divisible by 5\r\n  If i mod 5 = 0 Then\r\n    msgbox &quot;Number &quot; &amp; i &amp; &quot; is divisible by 5&quot;\r\n  End If\r\n\r\nNext\r\n<\/pre>\n<p><em><\/em><br \/>\n<strong>Example 2: Using Select Case with For Loop<\/strong><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Find all the dates that fall on Saturday or Sunday in March 2012\r\n\r\nDim dtDate,sDay\r\ndtDate = #03\/01\/2012#\r\n\r\n'Loop through all the days in the month of March\r\nFor i=1 to 31\r\n\r\n  'Find the Week Day for the Date\r\n  sDay = WeekDay(dtDate)\r\n\r\n  'Check if the Day is Saturday or Sunday\r\n  Select Case sDay\r\n    Case 1 : msgbox dtDate &amp; &quot; falls on Sunday&quot;\r\n    Case 7 : msgbox dtDate &amp; &quot; falls on Saturday&quot;\r\n  End Select\r\n\r\n  'Increment Date by One\r\n  dtDate = dtDate + 1\r\n\r\nNext\r\n<\/pre>\n<p><span><br \/>\n<\/span><\/p>\n<h3>Using Exit Statement in Loops<\/h3>\n<p>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 &#8211;<br \/>\n1) <strong>Exit For<\/strong> statement, can be used with <strong>For&#8230;Next<\/strong> and <strong>For Each&#8230;Next<\/strong> loops.<br \/>\n2) <strong>Exit Do<\/strong> statement, can be used with <strong>Do&#8230;Loop<\/strong>.<br \/>\n<em><\/em><br \/>\nLet&#8217;s see an example where the code exits the For&#8230;Next and Do&#8230;Loop based on some criteria.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Using Exit statements in loops\r\n\r\n'Set up infinite do loop\r\nDo\r\n\r\n  For i=1 to 100\r\n\r\n    'Create a Random Number\r\n    iRandom = Int(Rnd*10)\r\n\r\n    'Exit For Loop when Random number is 5 and Do loop when Random number is 9\r\n    Select Case iRandom\r\n      Case 5 : Exit For\r\n      Case 9 : Exit Do\r\n    End Select\r\n\r\n  Next\r\n\r\nLoop\r\n\r\nmsgbox &quot;Loops Exited&quot;\r\n<\/pre>\n<p>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 <a title=\"QTP VBScript Tutorial\" href=\"http:\/\/www.automationrepository.com\/category\/qtp-and-vbscript-tutorials\/\" target=\"_blank\">QTP VBScript tutorial<\/a> series. You can also leave a comment if you would like any other details to be covered here.<br \/>\n<span><br \/>\n<\/span><\/p>\n<\/div>\n<div style=\"font-family: Verdana,sans-serif; border: 1px solid #C8B560; padding: 3mm; background: #FFF8C6; text-align: center;\">\n<p><strong>If you enjoyed this article, you can join our blog to get new articles delivered directly in your inbox.<\/strong><\/p>\n<form style=\"text-align: center;\" action=\"http:\/\/feedburner.google.com\/fb\/a\/mailverify\" method=\"post\" onsubmit=\"window.open('http:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=automationrepository\/feeds', 'popupwindow', 'scrollbars=yes,width=550,height=520');return true\" target=\"popupwindow\"><input style=\"width: 160px; height: 20px;\" onfocus=\"if (this.value == 'Enter Your Email Address') {this.value = '';}\" onblur=\"if (this.value == '') {this.value = 'Enter Your Email Address';}\" type=\"text\" value=\"Enter Your Email Address\" \/> <input type=\"hidden\" name=\"uri\" value=\"automationrepository\/feeds\" \/> <input type=\"hidden\" name=\"loc\" value=\"en_US\" \/> <input type=\"submit\" value=\"Join Us\" \/><\/p>\n<p style=\"text-align: justify;\">\n<\/form>\n<\/div>\n<p style=\"border: 1px solid #C38EC7; padding: 3mm; background: #EBDDE2;\">To check out more tutorials, visit our <a title=\"QTP Tutorials\" href=\"http:\/\/www.automationrepository.com\/tutorials-for-qtp-beginners\/\" target=\"_blank\">QTP Tutorials<\/a> page. You can also check the <a title=\"All Articles\" href=\"http:\/\/www.automationrepository.com\/archive\/\" target=\"_blank\">Archives<\/a> page to view the list of all our articles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s see how this<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64,37],"tags":[85,84,83],"class_list":["post-1271","post","type-post","status-publish","format-standard","hentry","category-qtp-and-vbscript-tutorials","category-qtp-tutorials-for-beginners","tag-exit-do","tag-exit-for","tag-nested-loops"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Part 6: QTP and VBScript | Nested Loops in QTP - XX<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anish Pillai\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/\",\"name\":\"Part 6: QTP and VBScript | Nested Loops in QTP - XX\",\"isPartOf\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#website\"},\"datePublished\":\"2012-03-01T14:45:13+00:00\",\"dateModified\":\"2012-03-01T14:48:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.automationrepository.com\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Part 6: QTP and VBScript | Nested Loops in QTP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#website\",\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/\",\"name\":\"XX\",\"description\":\"\\r\\nasas\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.automationrepository.com\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20\",\"name\":\"Anish Pillai\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g\",\"caption\":\"Anish Pillai\"},\"description\":\"Find more about Anish Pillai on Google+\",\"sameAs\":[\"http:\/\/www.automationrepository.com\"],\"url\":\"https:\/\/www.automationrepository.com\/wordpress\/author\/anish\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Part 6: QTP and VBScript | Nested Loops in QTP - XX","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/","twitter_misc":{"Written by":"Anish Pillai","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/","url":"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/","name":"Part 6: QTP and VBScript | Nested Loops in QTP - XX","isPartOf":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#website"},"datePublished":"2012-03-01T14:45:13+00:00","dateModified":"2012-03-01T14:48:18+00:00","author":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20"},"breadcrumb":{"@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.automationrepository.com\/wordpress\/2012\/03\/qtp-and-vbscript-nested-loops-in-qtp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.automationrepository.com\/wordpress\/"},{"@type":"ListItem","position":2,"name":"Part 6: QTP and VBScript | Nested Loops in QTP"}]},{"@type":"WebSite","@id":"https:\/\/www.automationrepository.com\/wordpress\/#website","url":"https:\/\/www.automationrepository.com\/wordpress\/","name":"XX","description":"\r\nasas","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.automationrepository.com\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/7a35710e1ce89e5fb481be88fcd6cd20","name":"Anish Pillai","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.automationrepository.com\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8aa984de2295c3c4078fa48f6ba5d91e7c849b1a27a11dca24c6f11dd673ba14?s=96&d=mm&r=g","caption":"Anish Pillai"},"description":"Find more about Anish Pillai on Google+","sameAs":["http:\/\/www.automationrepository.com"],"url":"https:\/\/www.automationrepository.com\/wordpress\/author\/anish\/"}]}},"_links":{"self":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1271"}],"version-history":[{"count":26,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1271\/revisions"}],"predecessor-version":[{"id":1298,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/posts\/1271\/revisions\/1298"}],"wp:attachment":[{"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.automationrepository.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}