{"id":4574,"date":"2014-01-14T23:09:39","date_gmt":"2014-01-14T15:09:39","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=4574"},"modified":"2018-06-24T16:46:59","modified_gmt":"2018-06-24T08:46:59","slug":"visual-basic-2013-lesson-15-looping","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-15-looping\/","title":{"rendered":"Visual Basic 2013 Lesson 15: Looping"},"content":{"rendered":"<h4 style=\"text-align: center;\"><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-14-making-decisions-using-select-case\/\">[Lesson 14]<\/a> &lt;&lt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-tutorial\/\">[Contents]<\/a> &gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-16-sub-procedures\/\">[Lesson 16]<\/a><\/h4>\n<p>A procedure in a computer program that runs repeatedly until meeting the certain condition is called looping.\u00a0A loop can go on repetitively as long as the processor and memory could support. \u00a0For example, a program that adds a series of numbers until the sum exceeds a certain value or a program that prompts the user to enter data repeatedly until he or she enters the word \u2018Finish\u2019.<\/p>\n<p>In Visual Basic 2013, there are three methods of Looping, \u00a0the <strong>For\u2026..Next<\/strong> loop, the<strong> Do <\/strong>loop<strong>, <\/strong>and the <strong>While\u2026..End While<\/strong> loop. All methods produce the same repetitive effects.<\/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=\"3914691604\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>15.1 Looping using For&#8230;Next Loop<\/h3>\n<p>The most common looping method in VB 2013 is the For&#8230;.Next loop. The structure of a For&#8230;Next loop is as shown below:<\/p>\n<pre style=\"font-size: 110%;\">For counter=startNumber to endNumber Step increment\r\n One or more statements\r\nNext\r\n<\/pre>\n<p>To exit a For\u2026..Next Loop, you can place the <strong>Exit For<\/strong> statement within the loop; it is normally used together with the If\u2026.Then statement. For its application, you can refer to example 15.1 d.<br \/>\n<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=\"3914691604\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\n<strong>Example 15.1 a<\/strong><\/p>\n<pre style=\"font-size: 110%;\">Dim counter as Integer\r\nFor counter=1 to 10\r\n ListBox1.Items.Add (counter)\r\nNext\r\n<\/pre>\n<p>* The program will enter number 1 to 10 into the list box.<\/p>\n<h4><strong>Example 15.1b<\/strong><\/h4>\n<pre style=\"font-size: 110%;\">Dim counter , sum As Integer\r\nFor counter=1 to 100 step 10\r\n sum+=counter\r\n ListBox1.Items.Add (sum)\r\nNext\r\n<\/pre>\n<p>* The programme will calculate the sum of the numbers as follows:<\/p>\n<p>sum=0+10+20+30+40+\u2026\u2026<\/p>\n<h4><strong>Example 15.1c<\/strong><\/h4>\n<pre style=\"font-size: 110%;\">Dim counter, sum As Integer\r\nsum = 1000\r\nFor counter = 100 To 5 Step -5\r\n sum \u2013 = 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-\u2026\u2026\u2026.<\/p>\n<h4><strong>Example 15.1d<\/strong><\/h4>\n<pre style=\"font-size: 110%;\">Dim n as Integer\r\nFor n=1 to 10\r\nIf n&gt;6 then\r\n Exit For\r\nEnd If\r\nElse\r\n ListBox1.Items.Add ( n)\r\nNext\r\nEnd If\r\nNext\r\n<\/pre>\n<p>The process will stop when n is greater than 6.<\/p>\n<h3>15.2 Looping using 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\nBlock of one or more statements\r\nLoop\r\n<\/pre>\n<p>b)<\/p>\n<pre style=\"font-size: 110%;\">Do\r\nBlock of one or more statements\r\nLoop While condition\r\n<\/pre>\n<p>c)<\/p>\n<pre style=\"font-size: 110%;\">Do Until condition\r\nBlock of one or more statements\r\nLoop\r\n<\/pre>\n<p>d)<\/p>\n<pre style=\"font-size: 110%;\">Do\r\nBlock of one or more statements\r\nLoop Until condition\r\n<\/pre>\n<p>Sometimes we need exit to exit a loop prematurely because a certain<br \/>\ncondition is fulfilled. The syntax to use is <strong>Exit Do<\/strong>. Let&#8217;s examine the following examples:<\/p>\n<h4><strong>Example 15.2(a)<\/strong><\/h4>\n<pre style=\"font-size: 110%;\">Do while counter &lt;=1000\r\n TextBox1.Text=counter\r\n counter +=1\r\nLoop\r\n<\/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<p>&nbsp;<\/p>\n<h4><strong>Example 15.2(b)<\/strong><\/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(\u201cn\u201d &amp; vbTab &amp; \u201cSum\u201d)\r\n ListBox1.Items.Add(\u201c\u2014\u2014\u2014\u2014\u2014\u2014\u2014-\u201d)\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>* The loop in the above example can be replaced by the following loop:<\/p>\n<pre style=\"font-size: 110%;\">Do Until n = 10\r\n n += 1\r\n sum += n\r\n ListBox1.Items.Add(n &amp; vbTab &amp; sum)\r\nLoop\r\n<\/pre>\n<p>The output is as shown in Figure 15.1<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4586\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg\" alt=\"vb2013_figure15.1\" width=\"300\" height=\"300\" \/><\/a><\/p>\n<p><strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Figure 15.1<\/strong><\/p>\n<h3>15.3 Looping using While&#8230;.End While Loop<\/h3>\n<p>The structure of a While\u2026.End While Loop is very similar to the Do Loop. it takes the following form:<\/p>\n<pre style=\"font-size: 110%;\">While conditions\r\n\r\n Visual Basic 2013 statements\r\n\r\nEnd While\r\n<\/pre>\n<p><strong>Example 15.3<\/strong><\/p>\n<pre style=\"font-size: 110%;\">Private Sub Button1_Click(sender As Object, e As 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(\"\u2014\u2014\u2014\u2014\u2014\u2014\u2014-\")\r\nWhile n &lt;&gt;10\r\n n += 1\r\n sum += n\r\n ListBox1.Items.Add(n &amp; vbTab &amp; sum)\r\nEnd While\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=\"2306771905\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\"><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-14-making-decisions-using-select-case\/\">[Lesson 14]<\/a>\u00a0&lt;&lt;\u00a0<a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-tutorial\/\">[Contents]<\/a>\u00a0&gt;&gt; <a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-16-sub-procedures\/\">[Lesson 16]<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 14] &lt;&lt; [Contents] &gt;&gt; [Lesson 16] A procedure in a computer program that runs repeatedly until meeting the certain condition is called looping.\u00a0A loop can go on repetitively as long as the processor and memory could support. \u00a0For example, a program that adds a series of numbers until the sum exceeds a certain value &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-15-looping\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2013 Lesson 15: 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-4574","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 2013 Lesson 15: Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This lesson explains and demonstrates the usage of looping in visual basic 2013\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2013 Lesson 15: Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This lesson explains and demonstrates the usage of looping in visual basic 2013\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html\" \/>\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=\"2018-06-24T08:46:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg\" \/>\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=\"3 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-2013-lesson-15-looping\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html\",\"name\":\"Visual Basic 2013 Lesson 15: Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg\",\"datePublished\":\"2014-01-14T15:09:39+00:00\",\"dateModified\":\"2018-06-24T08:46:59+00:00\",\"description\":\"This lesson explains and demonstrates the usage of looping in visual basic 2013\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg\",\"width\":300,\"height\":300},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2013 Lesson 15: 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 2013 Lesson 15: Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This lesson explains and demonstrates the usage of looping in visual basic 2013","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":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2013 Lesson 15: Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This lesson explains and demonstrates the usage of looping in visual basic 2013","og_url":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html","og_site_name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","article_publisher":"https:\/\/www.facebook.com\/Vbtutor","article_modified_time":"2018-06-24T08:46:59+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2013-lesson-15-looping\/","url":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html","name":"Visual Basic 2013 Lesson 15: Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg","datePublished":"2014-01-14T15:09:39+00:00","dateModified":"2018-06-24T08:46:59+00:00","description":"This lesson explains and demonstrates the usage of looping in visual basic 2013","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html#primaryimage","url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg","contentUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg","width":300,"height":300},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2013\/vb2013_lesson15.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2013 Lesson 15: 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\/4574","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=4574"}],"version-history":[{"count":47,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4574\/revisions"}],"predecessor-version":[{"id":13069,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/4574\/revisions\/13069"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=4574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=4574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=4574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}