{"id":22282,"date":"2013-05-13T11:32:34","date_gmt":"2013-05-13T06:02:34","guid":{"rendered":"http:\/\/vskills.in\/certification\/tutorial\/?p=22282"},"modified":"2024-04-12T14:16:47","modified_gmt":"2024-04-12T08:46:47","slug":"the-while-do-for-and-foreach-loop","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/","title":{"rendered":"The while, do, for and foreach loop"},"content":{"rendered":"<h4><strong>The while, do, for and foreach loop<\/strong><\/h4>\n<p><strong>While<\/strong><\/p>\n<p>It is a control flow statement for repeated execution of code when a given condition evaluates to true.<\/p>\n<p>The syntax is as<\/p>\n<p>while (expression)<br \/>\n{<br \/>\nstatement(s);<br \/>\n}<\/p>\n<p>While execute all of the statements enclosed by in the while block as specified by the curly brackets. All of the statements are repeatedly executed till the expression evaluates to true. As for example to find the sum of values from a range of numbers is given below<\/p>\n<p>using System;<\/p>\n<p>public class Whileloop<br \/>\n{<br \/>\nstatic void Main()<br \/>\n{<br \/>\nint i = 0, sum = 0;<\/p>\n<p>while (i &lt; 10)<br \/>\n{<br \/>\ni++;<br \/>\nsum = sum + i;<br \/>\n}<br \/>\nConsole.WriteLine(sum);<br \/>\n}<br \/>\n}<\/p>\n<p>As in the example above, while consists of three main parts of Initialization, test and update. Each execution of the statement is called a cycle.<\/p>\n<p>int i = 0;<\/p>\n<p>The i variable is initiated and used as a counter.<\/p>\n<p>while (i &lt; 10)<br \/>\n{<br \/>\n&#8230;<br \/>\n}<\/p>\n<p>The expression inside the square brackets following the while keyword is the second phase, the testing. The statements in the body are executed, until the expression is evaluated to false.<\/p>\n<p>i++;<\/p>\n<p>The last, third phase of the while loop. The updating. We increment the counter. Note that improper handling of the while loops may lead to endless cycles.<\/p>\n<p>It is possible to run the statement at least once. Even if the condition is not met. For this, we can use the do while keywords.<\/p>\n<p>using System;<\/p>\n<p>public class CSharpApp<br \/>\n{<br \/>\nstatic void Main()<br \/>\n{<br \/>\nint count = 0;<\/p>\n<p>do {<br \/>\nConsole.WriteLine(count);<br \/>\n} while (count != 0);<br \/>\n}<br \/>\n}<\/p>\n<p>First the iteration is executed and then the truth expression is evaluated.<\/p>\n<p><strong>For<\/strong><\/p>\n<p>When the number of cycles is know before the loop is initiated, we can use the for statement. In this construct we declare a counter variable, which is automatically increased or decreased in value during each repetition of the loop.<\/p>\n<p>using System;<\/p>\n<p>public class CSharpApp<br \/>\n{<br \/>\nstatic void Main()<br \/>\n{<br \/>\nfor (int i=0; i&lt;9; i++)<br \/>\n{<br \/>\nConsole.WriteLine(i);<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>In this example, we print numbers 0..9 to the console.<\/p>\n<p>for (int i=0; i&lt;9; i++)<br \/>\n{<br \/>\nConsole.WriteLine(i);<br \/>\n}<\/p>\n<p>There are three phases. First, we initiate the counter i to zero. This phase is done only once. Next comes the condition. If the condition is met, the statement inside the for block is executed. Then comes the third phase; the couter is increased. Now we repeat the 2, 3 phases until the condition is not met and the for loop is left. In our case, when the counter i is equal to 9, the for loop stops executing.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>foreach<\/strong><\/span><\/p>\n<p>The foreach construct simplifies traversing over collections of data. It has no explicit counter. The foreach statement goes through the array or collection one by one and the current value is copied to a variable defined in the construct.<\/p>\n<p>using System;<\/p>\n<p>public class CSharpApp<br \/>\n{<br \/>\nstatic void Main()<br \/>\n{<br \/>\nstring[] planets = { &#8220;Mercury&#8221;, &#8220;Venus&#8221;,<br \/>\n&#8220;Earth&#8221;, &#8220;Mars&#8221;, &#8220;Jupiter&#8221;, &#8220;Saturn&#8221;,<br \/>\n&#8220;Uranus&#8221;, &#8220;Neptune&#8221; };<\/p>\n<p>foreach (string planet in planets)<br \/>\n{<br \/>\nConsole.WriteLine(planet);<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>In this example, we use the foreach statement to go through an array of planets.<\/p>\n<p>foreach (string planet in planets)<br \/>\n{<br \/>\nConsole.WriteLine(planet);<br \/>\n}<\/p>\n<p>The usage of the foreach statement is straightforward. The planets is the array, that we iterate through. The planet is the temporary variable, that has the current value from the array. The foreach statement goes through all the planets and prints them to the console.<\/p>\n<p>$ .\/planets.exe<br \/>\nMercury<br \/>\nVenus<br \/>\nEarth<br \/>\nMars<br \/>\nJupiter<br \/>\nSaturn<br \/>\nUranus<br \/>\nNeptune<\/p>\n<p>Running the above C# program gives this output.<\/p>\n<div class=\"apply\">\n<h3>Apply for C Sharp Certification Now!!<\/h3>\n<p><a href=\"https:\/\/www.vskills.in\/certification\/Certified-C-sharp-Professional\" target=\"_blank\" rel=\"noopener\">https:\/\/www.vskills.in\/certification\/Certified-C-sharp-Professional<\/a><\/p>\n<h4><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-c-professional\/\" target=\"_blank\" rel=\"noopener\"><strong>Back to Tutorial<\/strong><\/a><\/h4>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The while, do, for and foreach loop While It is a control flow statement for repeated execution of code when a given condition evaluates to true. The syntax is as while (expression) { statement(s); } While execute all of the statements enclosed by in the while block as specified by the curly brackets. All of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[3351],"tags":[],"class_list":["post-22282","page","type-page","status-publish","hentry","category-c-sharp"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The while, do, for and foreach loop - Tutorial<\/title>\n<meta name=\"description\" content=\"The while, do, for and foreach loop. Govt of India Certification for C#-Professional. Get Certified and improve employability. Certification assesses candidates in C#.\" \/>\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.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The while, do, for and foreach loop - Tutorial\" \/>\n<meta property=\"og:description\" content=\"The while, do, for and foreach loop. Govt of India Certification for C#-Professional. Get Certified and improve employability. Certification assesses candidates in C#.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:46:47+00:00\" \/>\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.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/\",\"name\":\"The while, do, for and foreach loop - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2013-05-13T06:02:34+00:00\",\"dateModified\":\"2024-04-12T08:46:47+00:00\",\"description\":\"The while, do, for and foreach loop. Govt of India Certification for C#-Professional. Get Certified and improve employability. Certification assesses candidates in C#.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The while, do, for and foreach loop\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The while, do, for and foreach loop - Tutorial","description":"The while, do, for and foreach loop. Govt of India Certification for C#-Professional. Get Certified and improve employability. Certification assesses candidates in C#.","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.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/","og_locale":"en_US","og_type":"article","og_title":"The while, do, for and foreach loop - Tutorial","og_description":"The while, do, for and foreach loop. Govt of India Certification for C#-Professional. Get Certified and improve employability. Certification assesses candidates in C#.","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:46:47+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/","name":"The while, do, for and foreach loop - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2013-05-13T06:02:34+00:00","dateModified":"2024-04-12T08:46:47+00:00","description":"The while, do, for and foreach loop. Govt of India Certification for C#-Professional. Get Certified and improve employability. Certification assesses candidates in C#.","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/the-while-do-for-and-foreach-loop\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"The while, do, for and foreach loop"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/22282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=22282"}],"version-history":[{"count":6,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/22282\/revisions"}],"predecessor-version":[{"id":132392,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/22282\/revisions\/132392"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=22282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=22282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=22282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}