{"id":197,"date":"2020-08-30T15:44:25","date_gmt":"2020-08-30T15:44:25","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=197"},"modified":"2020-08-30T15:44:25","modified_gmt":"2020-08-30T15:44:25","slug":"c-loops","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-loops\/","title":{"rendered":"C++ Loops"},"content":{"rendered":"<p>A loop allows you to execute a statement or groups of statements multiple times based on certain condition. In this lesson, we would consider four types of loops. We would also examine a few loop control statements.<\/p>\n<ol>\n<li><a href=\"#t1\">while Loop<\/a><\/li>\n<li><a href=\"#t2\">for Loop<\/a><\/li>\n<li><a href=\"#t3\">do..while Loop<\/a><\/li>\n<li><a href=\"#t4\">Nested Loop<\/a><\/li>\n<li><a href=\"#t5\">Loop Control Statements<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. while Loop<\/strong><\/h4>\n<p>A while loop is used to execute a statement or group of statements repeatedly so long as a given condition remains true.<\/p>\n<p>The syntax is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">while(condition) {\r\n   statement(s);\r\n}\r\n<\/pre>\n<p>The flow diagram for the while loop is shown below:<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/While-Loop.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-198 size-medium aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/While-Loop-250x300.jpg\" alt=\"While Loop\" width=\"250\" height=\"300\" srcset=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/While-Loop-250x300.jpg 250w, https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/While-Loop.jpg 556w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>The code below is an example of the while loop.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> x <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n\r\n   <span style=\"color: #888888;\">\/\/ while loop<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">while<\/span>( x <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span> ) {\r\n\t   <span style=\"color: #888888;\">\/\/Statement<\/span>\r\n      cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"x =  \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> x <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n      x<span style=\"color: #333333;\">++<\/span>;\r\n   } \r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>If you run the above code,\u00a0 you&#8217;ll have the output:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">x =  0\r\nx =  1\r\nx =  2\r\nx =  3\r\nx =  4\r\nx =  5\r\nx =  6\r\nx =  7\r\nx =  8\r\nx =  9\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. for Loop<\/strong><\/h4>\n<p>In the case of a for loop, the\u00a0 statements are executed a specific number of times. So, the number times the loop with iterate is specified. The basic syntax of the for loop is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">for ( init; condition; increment ) {\r\n   statements;\r\n}\r\n<\/pre>\n<ul>\n<li><strong>init<\/strong> is the loop variable<\/li>\n<li><strong>condition<\/strong> is the conditional statement to the checked each time. Normally\u00a0 the check is if the loop variable have reached certain threshold<\/li>\n<li><strong>increment<\/strong> is by what step to increase the loop variable after each iteration.<\/li>\n<\/ul>\n<p>The flow diagram for the for loop is given below:<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/For-Loop.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-199 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/For-Loop-217x300.jpg\" alt=\"For Loop\" width=\"217\" height=\"300\" srcset=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/For-Loop-217x300.jpg 217w, https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/For-Loop.jpg 633w\" sizes=\"auto, (max-width: 217px) 100vw, 217px\" \/><\/a><\/p>\n<p>The code below illustrates the for loop<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n   <span style=\"color: #888888;\">\/\/ for loop<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">for<\/span>( <span style=\"color: #333399; font-weight: bold;\">int<\/span> x <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>; x <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>; x <span style=\"color: #333333;\">=<\/span> x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span> ) {\r\n      cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"value of x is \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> x <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   }\r\n \r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>When code execute, x is displayed 10 times as well<\/p>\n<p>Note that in the for loop, the loop variable is declared and initialized inside the for statement.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. do..while Loop<\/strong><\/h4>\n<p>Similar to the while loop, the do&#8230;while loop executes statements a number of times. However, unlike the while loop, the condition is tested at the end of the loop. Therefore, this loop is guaranteed to execute at least, once.<\/p>\n<p>Basic syntax for the do..while loop is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">do {\r\n   statement;\r\n} \r\nwhile( condition );\r\n<\/pre>\n<p>The flow diagram is shown below:<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Do-While-Loop.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-200 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Do-While-Loop-241x300.jpg\" alt=\"Do While Loop\" width=\"241\" height=\"300\" srcset=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Do-While-Loop-241x300.jpg 241w, https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Do-While-Loop.jpg 490w\" sizes=\"auto, (max-width: 241px) 100vw, 241px\" \/><\/a><\/p>\n<p>An example of the do&#8230;while loop is given below.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n   <span style=\"color: #888888;\">\/\/ Local variable, x<\/span>\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> x <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n\r\n   <span style=\"color: #888888;\">\/\/ do loop statements<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">do<\/span> {\r\n      cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"value of x is \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> x <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n      x <span style=\"color: #333333;\">=<\/span> x <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>;\r\n   } <span style=\"color: #008800; font-weight: bold;\">while<\/span>( x <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span> );\r\n \r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>As before, if you run this code,\u00a0 x is printed 10 times from 0 to 9.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Nested Loop<\/strong><\/h4>\n<p>This is simply a situation when you nest a loop inside another loop. For example, we want to print the running sums of number between 1 and 10. So like this:<\/p>\n<ul>\n<li>1 = <strong>1<\/strong><\/li>\n<li>1 + 2 = <strong>3<\/strong><\/li>\n<li>1 + 2 + 3 = <strong>6<\/strong><\/li>\n<li>1 + 2 + 3 + 4 = <strong>10<\/strong><\/li>\n<li>&#8230;<\/li>\n<\/ul>\n<p>So the output would be 1, 3, 6, 10,&#8230;.<\/p>\n<p>Can you attempt it! Let&#8217;s use two nested for loop.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> sum;\r\n\r\n\t<span style=\"color: #888888;\">\/\/External loop<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">for<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>; i <span style=\"color: #333333;\">&lt;=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>; i<span style=\"color: #333333;\">++<\/span>) {\r\n\t\tsum <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n\t\t<span style=\"color: #888888;\">\/\/Nested internal loop<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">for<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> j <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>; j <span style=\"color: #333333;\">&lt;=<\/span> i; j<span style=\"color: #333333;\">++<\/span>){\r\n\t\t\tsum <span style=\"color: #333333;\">=<\/span> sum <span style=\"color: #333333;\">+<\/span> j;\r\n\t\t}\r\n\t\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> sum <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\", \"<\/span>;\r\n\t}\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>The output of the code would be:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">1, 3, 6, 10, 15, 21, 28, 36, 45, 55, \r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Loop Control Statements<\/strong><\/h4>\n<p>Discussion\u00a0 on loop is not complete without learning about loop control statements.<\/p>\n<p>Loop control statements allow you to alter the normal loop execution sequence. For instance, you saw from<a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-conditional-statements\/#t4\" target=\"_blank\" rel=\"noopener noreferrer\"> switch statement<\/a> that we used break statement to exit from the switch statements. Lets\u00a0 look at three loop control statements.<\/p>\n<p><strong>break statement<\/strong> &#8211; this statement is used to completely terminate the loop and exit to the statements following the loop<\/p>\n<p><strong>continue statement<\/strong> &#8211; this statement is used to terminate the current iteration of the loop<\/p>\n<p><strong>goto statement<\/strong> &#8211; this statement is used to transfer control to another part of the program with a label. Goto statements are not recommended for most scenarios.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A loop allows you to execute a statement or groups of statements multiple times based on certain condition. In this lesson, we would consider four &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-197","post","type-post","status-publish","format-standard","hentry","category-c-tutorials"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":201,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/197\/revisions\/201"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}