{"id":81405,"date":"2021-07-03T09:00:45","date_gmt":"2021-07-03T03:30:45","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=81405"},"modified":"2021-07-03T09:00:45","modified_gmt":"2021-07-03T03:30:45","slug":"loops-in-cpp","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/","title":{"rendered":"Loops in C++"},"content":{"rendered":"<p>When we need to run a set of statements repeatedly in programming, we utilize loops.<\/p>\n<p>Suppose if we want to print TechVidvan 8 times, we can write cout &lt;&lt; \u201cTechVidvan\u201d; 8 times iteratively. An alternate and efficient way of doing this is to use loops. We will have to write the statement (cout &lt;&lt; \u201cTechVidvan\u201d;) once inside a loop and the loop will run 8 times generating the desired result.<\/p>\n<p>A loop is a set of instructions that is repeated until a given condition is met. In this article, we will be covering loops in C++.<\/p>\n<h3>Types of loops in C++<\/h3>\n<h4>1. Entry Controlled loops in C++<\/h4>\n<p>Loops in which the condition is checked at the beginning of the loop are called Entry Controlled loops. There are 2 entry controlled loops in C++.<\/p>\n<h5>a.\u00a0 for loop in C++<\/h5>\n<p>We use the for loop when we know the exact number of times we want to iterate through a section of code.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">for (initialization; condition; update)\n{    \n     \/\/ body of the loop\n}\n<\/pre>\n<p>Here,<\/p>\n<ul>\n<li>initialization expression initializes the loop variable(s) and is executed only once.<\/li>\n<li>condition expression states the test condition. Loop body is executed if the condition is true; if it is false, the for loop is ended.<\/li>\n<li>update expression updates the value of loop variable(s) after execution of loop body.<\/li>\n<\/ul>\n<h5>Flowchart of for loop<\/h5>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/flow-chart-of-for-loop.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81697\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/flow-chart-of-for-loop.jpg\" alt=\"flow chart of for loop\" width=\"853\" height=\"1080\" \/><\/a><\/p>\n<p><strong>Example of for loop in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/Print first n natural numbers\n#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int n;\n  cout&lt;&lt;\"Enter n:\";\n  cin&gt;&gt;n;\n  cout&lt;&lt;endl&lt;&lt;\"First \"&lt;&lt;n&lt;&lt;\" natural numbers are:\"&lt;&lt;endl;\n  for(int i=1; i&lt;=n; i++) {\n      cout&lt;&lt;i&lt;&lt;\" \";\n  }\n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Enter n:10<br \/>\nFirst 10 natural numbers are:<br \/>\n1 2 3 4 5 6 7 8 9 10<\/div>\n<h5>b.\u00a0 while loop<\/h5>\n<p>while loops are useful when the exact number of loop iterations is unknown ahead of time. The loop executes as long as the test condition is true.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">while (condition)\n{\n  \/\/body of the loop\n}\n<\/pre>\n<h5>Flowchart of while loop<\/h5>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/Flowchart-of-while-loop.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81698\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/Flowchart-of-while-loop.jpg\" alt=\"Flowchart of while loop\" width=\"853\" height=\"875\" \/><\/a><\/p>\n<p><strong>Example of while loop in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/program to print TechVidvan 5 times\n\n#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int n = 1;\n  while(n&lt;=5) {\n      cout&lt;&lt;\"TechVidvan\"&lt;&lt;endl;\n      n++;\n  }\n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">TechVidvan<br \/>\nTechVidvan<br \/>\nTechVidvan<br \/>\nTechVidvan<br \/>\nTechVidvan<\/div>\n<h4>2. Exit Controlled Loop in C++<\/h4>\n<p>Loops in which the condition is checked at the end of the loop are called Exit Controlled loops. Hence, regardless of whether the condition is true or false, the loop body will run at least once.<\/p>\n<h5>a.\u00a0 do-while loop<\/h5>\n<p>A variant of while loop is do &#8211; while loop. But, in do &#8211; while loop, the set of statements will execute once before the condition is checked. This is ideal in situations when the loop must run at least once.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">do {\n  \/\/body of the loop\n} while (condition);\n<\/pre>\n<h5>Flowchart of do-while loop<\/h5>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/Flowchart-of-do-while-loop.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81699\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/Flowchart-of-do-while-loop.jpg\" alt=\"Flowchart of do-while loop\" width=\"853\" height=\"864\" \/><\/a><\/p>\n<p><strong>Example of the do-while loop in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int i=6;\n  do {\n      cout&lt;&lt;\"TechVidvan\"&lt;&lt;endl;\n      i++;\n  }while (i&lt;5);       \n  \n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">TechVidvan<\/div>\n<h4>3. Infinite Loop in C++<\/h4>\n<p>An infinite loop or an endless loop is a block of code that iterates indefinitely. This happens when the test condition evaluates to true every time. It is mostly an error.<\/p>\n<p><strong>Examples of situations of infinite loop in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">for (int i=1; i&gt;0; i++) {\n  cout&lt;&lt;\"Infinite loop\"&lt;&lt;endl;\n}\n\nfor ( ; ; ) {\ncout&lt;&lt;\"Infinite loop\"&lt;&lt;endl;\n}\n\nwhile(true) {\n  \/\/body of the loop\n}\n\ndo {\n  int i = 1;\n  \/\/statements\n} while(i&gt;0);\n<\/pre>\n<p>We can terminate an infinite loop by pressing Ctrl+c.<\/p>\n<h4>4. Nested Loop in C++<\/h4>\n<p>Nested loop, also called loop inside loop, is a loop contained within another loop.<\/p>\n<p><strong>Example of Nested loop in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/program to print a pattern\n#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  for(int i=0; i&lt;2; i++) {\n      for(int j=0; j&lt;3; j++) {\n          cout&lt;&lt;\"*\";\n      }\n      cout&lt;&lt;endl;\n  }\n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">***<br \/>\n***<\/div>\n<p>In this example, a for loop is nested inside another. However, different types of loops can be nested as well.<\/p>\n<h3>Loop Control Statements in C++<\/h3>\n<p>C++ offers some statements that we can use when we want to change the execution sequence of our program. All automatic objects generated in a scope are deleted when execution exits that scope.<\/p>\n<p>There are three loop control statements in C++:<br \/>\n1. <strong>break<\/strong>: We use the break to terminate a loop or a switch statement. Execution shifts to the next statement after the loop.<\/p>\n<p>2. <strong>continue<\/strong>: Continue statement skips the rest of the loop body and immediately checks the test condition before reiterating.<\/p>\n<p>3. <strong>goto<\/strong>: We use the goto statement to pass control to the labeled statement. Although using the goto statement is not recommended.<\/p>\n<h3>Summary<\/h3>\n<p>In this article, we learnt various types of loops in C++. Loops in C++ are broadly classified into entry controlled and exit controlled. There are two entry controlled loops namely, for loop and while loop. do-while loop is an exit controlled loop. We understood the use of these loops with proper examples.<\/p>\n<p>Then, we saw what infinite loops are in C++. We also covered nested loops with an example. Lastly, we learned about various loop control statements that C++ offers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we need to run a set of statements repeatedly in programming, we utilize loops. Suppose if we want to print TechVidvan 8 times, we can write cout &lt;&lt; \u201cTechVidvan\u201d; 8 times iteratively. An&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":81695,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[3681,3682,3683,3608,3609,3684,3614],"class_list":["post-81405","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-entry-controlled-loops","tag-exit-controlled-loop","tag-infinite-loop-in-c","tag-loop-control-statements-in-c","tag-loops-in-c","tag-nested-loop-in-c","tag-types-of-loops-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Loops in C++ - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn various types of loops in C++ with examples like entry controlled loops, exit controlled loops and infinite loops.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Loops in C++ - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn various types of loops in C++ with examples like entry controlled loops, exit controlled loops and infinite loops.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-03T03:30:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Types-of-loops-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Loops in C++ - TechVidvan","description":"Learn various types of loops in C++ with examples like entry controlled loops, exit controlled loops and infinite loops.","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:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Loops in C++ - TechVidvan","og_description":"Learn various types of loops in C++ with examples like entry controlled loops, exit controlled loops and infinite loops.","og_url":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-03T03:30:45+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Types-of-loops-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Loops in C++","datePublished":"2021-07-03T03:30:45+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/"},"wordCount":661,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Types-of-loops-in-C.jpg","keywords":["Entry Controlled loops","Exit Controlled Loop","Infinite Loop in C++","Loop Control Statements in C","Loops in C","Nested Loop in C++","Types of Loops in C"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/","url":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/","name":"Loops in C++ - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Types-of-loops-in-C.jpg","datePublished":"2021-07-03T03:30:45+00:00","description":"Learn various types of loops in C++ with examples like entry controlled loops, exit controlled loops and infinite loops.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Types-of-loops-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Types-of-loops-in-C.jpg","width":1200,"height":628,"caption":"Types of loops in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/loops-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Loops in C++"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=81405"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81405\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/81695"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=81405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=81405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=81405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}