{"id":79,"date":"2026-05-20T05:10:08","date_gmt":"2026-05-20T05:10:08","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=79"},"modified":"2026-05-22T07:48:49","modified_gmt":"2026-05-22T07:48:49","slug":"for-loop","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=for-loop","title":{"rendered":"for Loop"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>for<\/code> loop in C++ is used to repeat a block of code a specific number of times. It is one of the most commonly used loops in programming because it provides a simple and organized way to handle repetition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a for Loop?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>for<\/code> loop is a control statement that repeatedly executes code while a condition remains true.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is mainly used when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The number of iterations is known<\/li>\n\n\n\n<li>Repetitive tasks need to be performed<\/li>\n\n\n\n<li>Traversing arrays or collections<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of for Loop<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>for (initialization; condition; update) {<br>    \/\/ code to execute<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding the Syntax<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Initialization<\/strong> \u2192 Runs once at the beginning<\/li>\n\n\n\n<li><strong>Condition<\/strong> \u2192 Checked before each iteration<\/li>\n\n\n\n<li><strong>Update<\/strong> \u2192 Changes the loop variable after each iteration<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example of for Loop<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br>    for (int i = 1; i &lt;= 5; i++) {<br>        cout &lt;&lt; i &lt;&lt; endl;<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>1<br>2<br>3<br>4<br>5<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How for Loop Works<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Variable is initialized<\/li>\n\n\n\n<li>Condition is checked<\/li>\n\n\n\n<li>Loop body executes if condition is true<\/li>\n\n\n\n<li>Update statement runs<\/li>\n\n\n\n<li>Process repeats until condition becomes false<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Example: Sum of Numbers<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br>    int sum = 0;<br><br>    for (int i = 1; i &lt;= 5; i++) {<br>        sum += i;<br>    }<br><br>    cout &lt;&lt; \"Sum = \" &lt;&lt; sum;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Nested for Loop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>for<\/code> loop can also be placed inside another <code>for<\/code> loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of Nested Loop<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br>    for (int i = 1; i &lt;= 3; i++) {<br>        for (int j = 1; j &lt;= 2; j++) {<br>            cout &lt;&lt; i &lt;&lt; \" \" &lt;&lt; j &lt;&lt; endl;<br>        }<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Infinite for Loop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If no condition is given, the loop runs forever.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (;;) {<br>    cout &lt;&lt; \"Infinite Loop\";<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use infinite loops carefully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why for Loop is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>for<\/code> loop is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reduces repetitive code<\/li>\n\n\n\n<li>Makes programs shorter and cleaner<\/li>\n\n\n\n<li>Improves efficiency<\/li>\n\n\n\n<li>Helps in array and data traversal<\/li>\n\n\n\n<li>Is widely used in algorithms and logic building<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a teacher calling attendance for 30 students:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The same action repeats for each student<\/li>\n\n\n\n<li>Instead of writing code 30 times, a loop handles repetition automatically<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is how a <code>for<\/code> loop works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>for<\/code> loop in C++ is a powerful control structure used for repeating tasks efficiently. It helps developers write cleaner, shorter, and more organized programs while handling repetitive operations easily.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">C++ Fundamentals (Beginner Level) > Loops > for Loop<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":16,"template":"","class_list":["post-79","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>for Loop - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ for loop with syntax, examples, nested loops, and use cases for repetition, arrays, and problem solving.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"for Loop - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ for loop with syntax, examples, nested loops, and use cases for repetition, arrays, and problem solving.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T07:48:49+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=for-loop\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"for Loop - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T05:10:08+00:00\",\"dateModified\":\"2026-05-22T07:48:49+00:00\",\"description\":\"Learn C++ for loop with syntax, examples, nested loops, and use cases for repetition, arrays, and problem solving.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Fundamentals (Beginner Level) > Loops > for Loop\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"for Loop - Learn C++Language with GiGz.PK","description":"Learn C++ for loop with syntax, examples, nested loops, and use cases for repetition, arrays, and problem solving.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"for Loop - Learn C++Language with GiGz.PK","og_description":"Learn C++ for loop with syntax, examples, nested loops, and use cases for repetition, arrays, and problem solving.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T07:48:49+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=for-loop","url":"https:\/\/gigz.pk\/","name":"for Loop - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T05:10:08+00:00","dateModified":"2026-05-22T07:48:49+00:00","description":"Learn C++ for loop with syntax, examples, nested loops, and use cases for repetition, arrays, and problem solving.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"C++ Fundamentals (Beginner Level) > Loops > for Loop"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}