{"id":81,"date":"2026-05-20T05:11:14","date_gmt":"2026-05-20T05:11:14","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=81"},"modified":"2026-05-22T07:54:32","modified_gmt":"2026-05-22T07:54:32","slug":"while-loop","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=while-loop","title":{"rendered":"while Loop"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>while<\/code> loop in C++ is used to repeatedly execute a block of code as long as a condition remains true. It is commonly used when the number of iterations is not known in advance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a while Loop?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>while<\/code> loop is a control statement that keeps running until its condition becomes false.<\/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 repetitions is unknown<\/li>\n\n\n\n<li>A condition controls the loop<\/li>\n\n\n\n<li>User input or runtime conditions are involved<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of while Loop<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>while (condition) {<br>    \/\/ code to execute<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How while Loop Works<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The condition is checked<\/li>\n\n\n\n<li>If the condition is true, the loop body executes<\/li>\n\n\n\n<li>After execution, the condition is checked again<\/li>\n\n\n\n<li>The loop continues until the condition becomes false<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Example of while 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>    int i = 1;<br><br>    while (i &lt;= 5) {<br>        cout &lt;&lt; i &lt;&lt; endl;<br>        i++;<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\">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 i = 1;<br>    int sum = 0;<br><br>    while (i &lt;= 5) {<br>        sum += i;<br>        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\">Infinite while Loop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If the condition never becomes false, the loop runs forever.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while (true) {<br>    cout &lt;&lt; \"Infinite Loop\";<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Always make sure the condition eventually becomes false.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between for and while Loop<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>for<\/code> loop is used when the number of iterations is known<\/li>\n\n\n\n<li><code>while<\/code> loop is used when the number of iterations is unknown<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example with User Input<\/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 number;<br><br>    cout &lt;&lt; \"Enter a number (0 to stop): \";<br>    cin &gt;&gt; number;<br><br>    while (number != 0) {<br>        cout &lt;&lt; \"You entered: \" &lt;&lt; number &lt;&lt; endl;<br><br>        cout &lt;&lt; \"Enter a number (0 to stop): \";<br>        cin &gt;&gt; number;<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why while Loop is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>while<\/code> loop is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Handles unknown repetitions<\/li>\n\n\n\n<li>Supports condition-based execution<\/li>\n\n\n\n<li>Is useful for user-driven programs<\/li>\n\n\n\n<li>Simplifies repetitive tasks<\/li>\n\n\n\n<li>Is widely used in real-world applications<\/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 filling water in a tank:<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779436499843\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>While the tank is not full, water continues filling<\/li>\n\n\n\n<li>Once full, the process stops<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is similar to how a <code>while<\/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>while<\/code> loop in C++ is a flexible and useful looping structure for repeating tasks based on conditions. It is widely used in programs where the number of iterations depends on runtime conditions or user input.<\/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 > while Loop<\/span><\/span><\/div>","protected":false},"menu_order":17,"template":"","class_list":["post-81","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>while Loop - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ while loop with syntax, examples, and use cases for condition-based repetition and user input handling.\" \/>\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=\"while Loop - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ while loop with syntax, examples, and use cases for condition-based repetition and user input handling.\" \/>\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:54:32+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=while-loop\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"while Loop - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T05:11:14+00:00\",\"dateModified\":\"2026-05-22T07:54:32+00:00\",\"description\":\"Learn C++ while loop with syntax, examples, and use cases for condition-based repetition and user input handling.\",\"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 > while 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":"while Loop - Learn C++Language with GiGz.PK","description":"Learn C++ while loop with syntax, examples, and use cases for condition-based repetition and user input handling.","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":"while Loop - Learn C++Language with GiGz.PK","og_description":"Learn C++ while loop with syntax, examples, and use cases for condition-based repetition and user input handling.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T07:54:32+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=while-loop","url":"https:\/\/gigz.pk\/","name":"while Loop - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T05:11:14+00:00","dateModified":"2026-05-22T07:54:32+00:00","description":"Learn C++ while loop with syntax, examples, and use cases for condition-based repetition and user input handling.","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 > while 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\/81","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=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}