{"id":6433,"date":"2015-04-06T09:31:07","date_gmt":"2015-04-06T01:31:07","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=6433"},"modified":"2018-06-22T18:02:35","modified_gmt":"2018-06-22T10:02:35","slug":"visual-basic-2015-lesson-15-looping-repetitive-jobs","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-15-looping-repetitive-jobs\/","title":{"rendered":"Visual Basic 2015 Lesson 15: Looping for Repetitive Jobs"},"content":{"rendered":"<h4 style=\"text-align: center;\"><a title=\"visual basic 2015 tutorial lesson 14\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-14-dealing-multiple-choices-using-select-case\/\">[Lesson 14]<\/a> &lt;&lt; <a title=\"visual basic 2015 tutorial\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-tutorial\/\">[Contents]<\/a> &gt;&gt;<a title=\"visual basic 2015 tutorial lesson 16\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-16-understanding-sub-procedures\/\"> [Lesson 16]<\/a><\/h4>\n<p>In Visual Basic 2015, looping is a process that involves a \u00a0procedure that runs repetitively until a certain condition is met.\u00a0For example, we can design a program that adds a series of numbers until the sum exceeds a certain value or a program that asks the user to enter data repeatedly until he or she enters the word \u2018Finish\u2019.\u00a0 There are three types of Loops in Visual Basic 2015, \u00a0namely the <strong>For\u2026..Next<\/strong> loop, the<strong> Do <\/strong>loop<strong>, <\/strong>and the <strong>While\u2026..End While<\/strong> 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=\"4768455349\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h2>15.1 For\u2026.Next Loop<\/h2>\n<p>In\u00a0Visual Basic 2015, the structure of a For&#8230;Next loop is as shown below:<br \/>\n<strong>For<\/strong> counter=startNumber<strong> to<\/strong> endNumber (<strong>Step increment<\/strong>)<br \/>\nOne or more Visual Basic 2015 statements<strong><br \/>\nNext<\/strong><br \/>\nIn order to exit a For\u2026..Next Loop, you need to 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.<\/p>\n<h4>Example 15.1 a<\/h4>\n<pre style=\"font-size: 110%;\">Dim counter as Integer\r\nFor counter=1 to 10\r\nListBox1.Items.Add (counter)\r\nNext\r\n<\/pre>\n<p>* The program will enter number 1 to 10 into the list box.<\/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=\"4768455349\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4><strong>Example 15.1b<\/strong><\/h4>\n<p>The following program will calculate the sum of the numbers 0+10+20+30+40+\u2026\u2026+100<\/p>\n<pre style=\"font-size: 110%;\">Dim counter , sum As Integer\r\nFor counter=1 to 100 step 10\r\nsum+=counter\r\nListBox1.Items.Add (sum)\r\nNext\r\n<\/pre>\n<h4><strong>Example 15.1c<\/strong><\/h4>\n<p>This program will compute a series of\u00a0subtractions as follow:<br \/>\n1000-100-95-90-\u2026\u2026\u2026-5.\u00a0In this case, the increment is negative.<\/p>\n<pre style=\"font-size: 110%;\">Dim counter, sum As Integer\r\n\r\nsum = 1000\r\nFor counter = 100 To 5 Step -5\r\nsum \u2013 = counter\r\nListBox1.Items.Add(sum)\r\n\r\nNext\r\n<\/pre>\n<h4><strong>Example 15.1d<\/strong><\/h4>\n<p>This program uses Exit &#8230;For to escape the loop when n is greater than 6.<\/p>\n<pre style=\"font-size: 110%;\">Dim n as Integer\r\n\r\nFor n=1 to 10\r\nIf n&gt;6 then\r\nExit For\r\nEnd If\r\nElse\r\nListBox1.Items.Add ( n)\r\nNext\r\nEnd If\r\nNext\r\n<\/pre>\n<h3><strong>15.2 Do Loop<\/strong><\/h3>\n<p>In Visual Basic 2015, there are several \u00a0Do Loop structures, as shown below:<br \/>\na)<\/p>\n<pre style=\"font-size: 110%;\">Do While condition\r\nBlock of statements\r\nLoop\r\n<\/pre>\n<p>b)<\/p>\n<pre style=\"font-size: 110%;\">Do\r\nBlock of statements\r\nLoop While condition\r\n<\/pre>\n<p>c)<\/p>\n<pre style=\"font-size: 110%;\">Do Until condition\r\nBlock of statements\r\nLoop\r\n<\/pre>\n<p>d)<\/p>\n<pre style=\"font-size: 110%;\">Do\r\nBlock of one or more Visual Basic 2015 statements\r\nLoop Until condition\r\n<\/pre>\n<p>* Exiting the Loop<\/p>\n<p>We can also use\u00a0<strong>Exit Do <\/strong>to escape the loop.<\/p>\n<p>Let&#8217; s examine the following examples:<\/p>\n<h4><strong>Example 15.2(a)<\/strong><\/h4>\n<p>In this example, the procedure\u00a0will keep on adding the initial number by 1 until it exceeds 1000.<\/p>\n<pre style=\"font-size: 110%;\">Do while counter &lt;=1000\r\n\r\nTextBox1.Text=counter\r\ncounter +=1\r\n\r\nLoop\r\n<\/pre>\n<p>We can rewrite the procedure above and achieve the same result. The code is shown as follows:<\/p>\n<pre style=\"font-size: 110%;\">Do\r\nTextBox1.Text=counter\r\ncounter+=1\r\nLoop until counter&gt;1000\r\n<\/pre>\n<h4><strong>Example 15.2(b)<\/strong><\/h4>\n<p>In this example, the procedure will keep on adding a number by 1 and display the results in a list box. The process stops when it has repeated 100 times.<\/p>\n<pre style=\"font-size: 110%;\">Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click\r\n\r\nDim sum, n As Integer\r\n\r\nListBox1.Items.Add(\u201cn\u201d &amp; vbTab &amp; \u201cSum\u201d)\r\nListBox1.Items.Add(\u201c\u2014\u2014\u2014\u2014\u2014\u2014\u2014-\u201d)\r\nDo\r\nn += 1\r\nsum += n\r\nListBox1.Items.Add(n &amp; vbTab &amp; sum)\r\nIf n = 100 Then\r\nExit Do\r\nEnd If\r\nLoop\r\n\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\nn += 1\r\nsum += n\r\nListBox1.Items.Add(n &amp; vbTab &amp; sum)\r\n\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<h2>15.3 While&#8230;.End While Loop<\/h2>\n<p>In Visual Basic 2015, the structure of a <strong>While\u2026.End While<\/strong> 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\nVisual Basic 2015 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\nListBox1.Items.Add(\"n\" &amp; vbTab &amp; \"sum\")\r\nListBox1.Items.Add(\"\u2014\u2014\u2014\u2014\u2014\u2014\u2014-\")\r\n\r\nWhile n &lt;&gt; 10\r\n\r\nn += 1\r\nsum += n\r\nListBox1.Items.Add(n &amp; vbTab &amp; sum)\r\n\r\nEnd While\r\n\r\nEnd Sub\r\n<\/pre>\n<p align=\"center\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"5090773108\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\"><a title=\"visual basic tutorial lesson 14\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-14-dealing-multiple-choices-using-select-case\/\">[Lesson 14]<\/a>\u00a0&lt;&lt;\u00a0<a title=\"visual basic 2015 tutorial\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-tutorial\/\">[Contents]<\/a>\u00a0&gt;&gt; <a title=\"visual basic 2015 tutorial lesson 16\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-16-understanding-sub-procedures\/\">[Lesson 16]<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 14] &lt;&lt; [Contents] &gt;&gt; [Lesson 16] In Visual Basic 2015, looping is a process that involves a \u00a0procedure that runs repetitively until a certain condition is met.\u00a0For example, we can design a program that adds a series of numbers until the sum exceeds a certain value or a program that asks the user to &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-15-looping-repetitive-jobs\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2015 Lesson 15: Looping for Repetitive Jobs<\/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-6433","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 2015 Lesson 15: Looping for Repetitive Jobs - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This article illustrates how the concept of looping in Visual Basic 2015\" \/>\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\/vb2015\/vb2015_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 2015 Lesson 15: Looping for Repetitive Jobs - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This article illustrates how the concept of looping in Visual Basic 2015\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_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-22T10:02:35+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-2015-lesson-15-looping-repetitive-jobs\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson15.html\",\"name\":\"Visual Basic 2015 Lesson 15: Looping for Repetitive Jobs - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson15.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson15.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg\",\"datePublished\":\"2015-04-06T01:31:07+00:00\",\"dateModified\":\"2018-06-22T10:02:35+00:00\",\"description\":\"This article illustrates how the concept of looping in Visual Basic 2015\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson15.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson15.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_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\/vb2015\/vb2015_lesson15.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2015 Lesson 15: Looping for Repetitive Jobs\"}]},{\"@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 2015 Lesson 15: Looping for Repetitive Jobs - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This article illustrates how the concept of looping in Visual Basic 2015","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\/vb2015\/vb2015_lesson15.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2015 Lesson 15: Looping for Repetitive Jobs - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This article illustrates how the concept of looping in Visual Basic 2015","og_url":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_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-22T10:02:35+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-2015-lesson-15-looping-repetitive-jobs\/","url":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson15.html","name":"Visual Basic 2015 Lesson 15: Looping for Repetitive Jobs - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson15.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson15.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg","datePublished":"2015-04-06T01:31:07+00:00","dateModified":"2018-06-22T10:02:35+00:00","description":"This article illustrates how the concept of looping in Visual Basic 2015","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson15.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson15.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_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\/vb2015\/vb2015_lesson15.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2015 Lesson 15: Looping for Repetitive Jobs"}]},{"@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\/6433","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=6433"}],"version-history":[{"count":54,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/6433\/revisions"}],"predecessor-version":[{"id":13030,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/6433\/revisions\/13030"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=6433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=6433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=6433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}