{"id":10031,"date":"2017-04-06T23:06:08","date_gmt":"2017-04-06T15:06:08","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=10031"},"modified":"2018-06-22T13:46:37","modified_gmt":"2018-06-22T05:46:37","slug":"visual-basic-2017-lesson-15-looping","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2017-lesson-15-looping\/","title":{"rendered":"Visual Basic 2017 Lesson 15: Looping"},"content":{"rendered":"<h4 style=\"text-align: center;\"><a title=\"visual basic 2017 tutorial lesson 14\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2017-lesson-14-using-select-case\/\">[Lesson 14]<\/a> &lt;&lt; <a title=\"visual basic 2017 tutorial\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2017-tutorial\/\">[Contents]<\/a> &gt;&gt;<a title=\"visual basic 2015 tutorial lesson 16\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2017-lesson-16-sub-procedures\/\"> [Lesson 16]<\/a><\/h4>\n<p>In Visual Basic 2017, looping 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 2017, \u00a0namely the <strong>For\u2026..Next<\/strong> loop, the<strong> Do <\/strong>loop<strong>, <\/strong>and the <strong>While\u2026..End While<\/strong> loop<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=\"3393818013\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3>15.1 For\u2026.Next Loop<\/h3>\n<p>In\u00a0Visual Basic 2017, the structure of a For&#8230;Next loop is as shown below:<\/p>\n<p><strong>For<\/strong> counter=startNumber<strong> to<\/strong> endNumber (<strong>Step increment<\/strong>)<\/p>\n<p>One or more Visual Basic 2017 statements<\/p>\n<p>Next<br \/>\nIn order to exit a For\u2026..Next Loop, you can place the <strong>Exit For<\/strong> statement within the loop. It is usually 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<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<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=\"3393818013\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\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\nsum = 1000\r\nFor counter = 100 To 5 Step -5\r\nsum \u2013 = counter\r\nListBox1.Items.Add(sum)\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\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 2017, there are several \u00a0Do Loop structures, as shown below:<br \/>\na) Do While condition<br \/>\nBlock of one or more Visual Basic 2017 statements<br \/>\nLoop<\/p>\n<p>b) Do<br \/>\nBlock of one or more Visual Basic 2017 statements<br \/>\nLoop While condition<\/p>\n<p>c) Do Until condition<br \/>\nBlock of one or more Visual Basic 2017 statements<br \/>\nLoop<\/p>\n<p>d) Do<br \/>\nBlock of one or more Visual Basic 2017 statements<br \/>\nLoop Until condition<\/p>\n<p>* Exiting the Loop<\/p>\n<p>We can also use\u00a0<strong>Exit Do <\/strong>to escape 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=\"3393818013\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nLet&#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\nTextBox1.Text=counter\r\ncounter +=1\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\nDim sum, n As Integer\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\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\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 While&#8230;.End While Loop<\/h3>\n<p>In Visual Basic 2017, 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\nVisual Basic 2017 statements\r\nEnd While\r\n<\/pre>\n<h4>Example 15.3<\/h4>\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\nn += 1\r\nsum += n\r\nListBox1.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<!-- VB2017 Responsive --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"4922050706\" data-ad-format=\"auto\"><\/ins><br \/>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\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=\"9961025909\"><\/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-2017-lesson-14-using-select-case\/\">[Lesson 14]<\/a>\u00a0&lt;&lt;\u00a0<a title=\"visual basic 2017 tutorial\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2017-tutorial\/\">[Contents]<\/a>\u00a0&gt;&gt; <a title=\"visual basic 2015 tutorial lesson 16\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2017-lesson-16-sub-procedures\/\">[Lesson 16]<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 14] &lt;&lt; [Contents] &gt;&gt; [Lesson 16] In Visual Basic 2017, looping 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 &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2017-lesson-15-looping\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2017 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-10031","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 2017 Lesson 15: Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"Learning to write looping code in visual basic 2017\" \/>\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\/vb2017\/vb2017_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 2017 Lesson 15: Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"Learning to write looping code in visual basic 2017\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2017\/vb2017_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-22T05:46:37+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-2017-lesson-15-looping\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2017\/vb2017_lesson15.html\",\"name\":\"Visual Basic 2017 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\/vb2017\/vb2017_lesson15.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2017\/vb2017_lesson15.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg\",\"datePublished\":\"2017-04-06T15:06:08+00:00\",\"dateModified\":\"2018-06-22T05:46:37+00:00\",\"description\":\"Learning to write looping code in visual basic 2017\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2017\/vb2017_lesson15.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2017\/vb2017_lesson15.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2017\/vb2017_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\/vb2017\/vb2017_lesson15.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2017 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 2017 Lesson 15: Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Learning to write looping code in visual basic 2017","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\/vb2017\/vb2017_lesson15.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2017 Lesson 15: Looping - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"Learning to write looping code in visual basic 2017","og_url":"http:\/\/www.vbtutor.net\/vb2017\/vb2017_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-22T05:46:37+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-2017-lesson-15-looping\/","url":"http:\/\/www.vbtutor.net\/vb2017\/vb2017_lesson15.html","name":"Visual Basic 2017 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\/vb2017\/vb2017_lesson15.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2017\/vb2017_lesson15.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure15.1.jpg","datePublished":"2017-04-06T15:06:08+00:00","dateModified":"2018-06-22T05:46:37+00:00","description":"Learning to write looping code in visual basic 2017","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2017\/vb2017_lesson15.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2017\/vb2017_lesson15.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2017\/vb2017_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\/vb2017\/vb2017_lesson15.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2017 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\/10031","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=10031"}],"version-history":[{"count":21,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/10031\/revisions"}],"predecessor-version":[{"id":12988,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/10031\/revisions\/12988"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=10031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=10031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=10031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}