{"id":2379,"date":"2013-02-13T12:16:10","date_gmt":"2013-02-13T04:16:10","guid":{"rendered":"http:\/\/www.vbtutor.net\/index.php\/"},"modified":"2017-10-20T22:20:19","modified_gmt":"2017-10-20T14:20:19","slug":"visual-basic-2012-lesson-11-looping","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/","title":{"rendered":"Visual Basic 2012 Lesson 11- Looping"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-10-using-select-case\/\">[Lesson 10]<\/a> <\/strong>&lt;&lt; <strong> <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\">[CONTENTS]<\/a> &gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-12-functions-part-1\/\">[Lesson 12]<\/a> <\/strong><\/h4>\n<p>Looping is required when we need to process something repetitively until a certain condition is met. For example, we can design a program that adds a series of numbers until the sum exceeds a certain value. We can also write a program that prompts the user to enter data repeatedly until he or she enters the word &#8216;Finish&#8217;. In Visual Basic 2012, there are three types of Loops, they are the For&#8230;..Next loop, the Do loop. and the While&#8230;..End while loop<\/p>\n<h3>11.1 Looping using the For&#8230;.Next Loop<\/h3>\n<p>The syntax is:<\/p>\n<pre style=\"font-size: 110%;\">For counter=startNumber to endNumber (Step increment)\r\n\r\n One or more VB statements\r\n\r\nNext\r\n<\/pre>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nTo exit a For&#8230;..Next Loop, you can place the Exit For statement within the loop, please refer to example 11.1 d.<\/p>\n<h4>Example 11.1 a<\/h4>\n<pre style=\"font-size: 110%;\">Dim counter as Integer\r\n For counter=1 to 10\r\n  ListBox1.Items.Add (counter)\r\n Next\r\n<\/pre>\n<p>* The program will enter number 1 to 10 into the list box.<\/p>\n<h4>Example 11.1b<\/h4>\n<pre style=\"font-size: 110%;\">Dim counter , sum As Integer\r\n For counter=1 to 100 step 10\r\n  sum+=counter\r\n  ListBox1.Items.Add (sum)\r\n Next\r\n<\/pre>\n<p>* The program will calculate the sum of the numbers as follows:<\/p>\n<p>sum=0+10+20+30+40+&#8230;&#8230;<\/p>\n<h4>Example 11.1c<\/h4>\n<pre style=\"font-size: 110%;\">Dim counter, sum As Integer\r\nsum = 1000\r\n For counter = 100 To 5 Step -5\r\n  sum - = counter\r\n  ListBox1.Items.Add(sum)\r\nNext\r\n<\/pre>\n<p>*Notice that increment can be negative.<\/p>\n<p>The program will compute the<br \/>\nsubtraction as follow:<br \/>\n1000-100-95-90-&#8230;&#8230;&#8230;.<\/p>\n<h4>Example 11.1d<\/h4>\n<pre style=\"font-size: 110%;\">Dim n as Integer\r\nFor n=1 to 10\r\n If n&gt;6 then\r\n  Exit For\r\n End If\r\n Else\r\n  ListBox1.Items.Add ( n)\r\nNext\r\n\r\n<\/pre>\n<p>The process will stop when n is greater than 6.<\/p>\n<h3>11.2 The Do Loop<\/h3>\n<p>The Do Loop structures are<\/p>\n<p>a)<\/p>\n<pre style=\"font-size: 110%;\">Do While condition\r\n Block of one or more statements\r\nLoop\r\n<\/pre>\n<p>b)<\/p>\n<pre style=\"font-size: 110%;\">Do\r\n Block of one or more statements\r\nLoop While condition\r\n<\/pre>\n<p>c)<\/p>\n<pre>Do Until condition\r\n Block of one or more statements\r\nLoop\r\n<\/pre>\n<p>d)<\/p>\n<pre style=\"font-size: 110%;\">Do\r\n Block of one or more statements\r\nLoop Until condition\r\n<\/pre>\n<p>* Exiting the Loop<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nSometimes we need to exit a loop prematurely because of a certain<br \/>\ncondition is fulfilled. The syntax to use is known as Exit Do. Let&#8217;s examine the following examples<\/p>\n<h4>Example 11.2(a)<\/h4>\n<pre style=\"font-size: 110%;\">Do while counter<\/pre>\n<p>* The above example will keep on adding until counter &gt;1000.<\/p>\n<p>The above example can be rewritten as<\/p>\n<pre style=\"font-size: 110%;\">Do\r\n TextBox1.Text=counter\r\n counter+=1\r\nLoop until counter&gt;1000\r\n<\/pre>\n<h4>Example 11.2(b)<\/h4>\n<pre style=\"font-size: 110%;\">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\r\nDim sum, n As Integer\r\n ListBox1.Items.Add(\"n\" &amp; vbTab &amp; \"Sum\")\r\n ListBox1.Items.Add(\"----------------------\")\r\nDo\r\n n += 1\r\n sum += n\r\n  ListBox1.Items.Add(n &amp; vbTab &amp; sum)\r\n If n = 100 Then\r\n  Exit Do\r\n End If\r\nLoop\r\nEnd Sub\r\n<\/pre>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-format=\"autorelaxed\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"6598395509\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-10-using-select-case\/\">[Lesson 10]<\/a> <\/strong>&lt;&lt; <strong> <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\">[CONTENTS]<\/a> &gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-12-functions-part-1\/\">[Lesson 12]<\/a> <\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 10] &lt;&lt; [CONTENTS] &gt;&gt; [Lesson 12] Looping is required when we need to process something repetitively until a certain condition is met. For example, we can design a program that adds a series of numbers until the sum exceeds a certain value. We can also write a program that prompts the user to enter &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2012 Lesson 11- Looping<\/span><\/a><\/p>\n","protected":false},"author":23013,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2379","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Visual Basic 2012 Lesson 11- Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This visual basic 2012 tutorial shows you how to write code for looping in visual basic 2012\" \/>\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.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2012 Lesson 11- Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This visual basic 2012 tutorial shows you how to write code for looping in visual basic 2012\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Vbtutor\" \/>\n<meta property=\"article:modified_time\" content=\"2017-10-20T14:20:19+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@liewvk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/\",\"url\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/\",\"name\":\"Visual Basic 2012 Lesson 11- Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"datePublished\":\"2013-02-13T04:16:10+00:00\",\"dateModified\":\"2017-10-20T14:20:19+00:00\",\"description\":\"This visual basic 2012 tutorial shows you how to write code for looping in visual basic 2012\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2012 Lesson 11- Looping\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vbtutor.net\/#website\",\"url\":\"https:\/\/www.vbtutor.net\/\",\"name\":\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"description\":\"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Visual Basic 2012 Lesson 11- Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This visual basic 2012 tutorial shows you how to write code for looping in visual basic 2012","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.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2012 Lesson 11- Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This visual basic 2012 tutorial shows you how to write code for looping in visual basic 2012","og_url":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/","og_site_name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","article_publisher":"https:\/\/www.facebook.com\/Vbtutor","article_modified_time":"2017-10-20T14:20:19+00:00","twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/","url":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/","name":"Visual Basic 2012 Lesson 11- Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"datePublished":"2013-02-13T04:16:10+00:00","dateModified":"2017-10-20T14:20:19+00:00","description":"This visual basic 2012 tutorial shows you how to write code for looping in visual basic 2012","breadcrumb":{"@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-11-looping\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2012 Lesson 11- Looping"}]},{"@type":"WebSite","@id":"https:\/\/www.vbtutor.net\/#website","url":"https:\/\/www.vbtutor.net\/","name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/users\/23013"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=2379"}],"version-history":[{"count":52,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2379\/revisions"}],"predecessor-version":[{"id":11028,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2379\/revisions\/11028"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=2379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=2379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=2379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}