{"id":83,"date":"2026-05-20T05:16:25","date_gmt":"2026-05-20T05:16:25","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=83"},"modified":"2026-05-22T07:57:40","modified_gmt":"2026-05-22T07:57:40","slug":"do-while-loop","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=do-while-loop","title":{"rendered":"do while Loop"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>do while<\/code> loop in C++ is a looping statement that executes a block of code at least once before checking the condition. It is useful when the code must run one time regardless of the condition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a do while Loop?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>do while<\/code> loop is a control structure that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Executes the loop body first<\/li>\n\n\n\n<li>Checks the condition afterward<\/li>\n\n\n\n<li>Repeats the loop while the condition is true<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike the <code>while<\/code> loop, the condition is checked after execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of do while Loop<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>do {<br>    \/\/ code to execute<br>} while (condition);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How do while Loop Works<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The loop body executes first<\/li>\n\n\n\n<li>The condition is checked<\/li>\n\n\n\n<li>If the condition is true, the loop repeats<\/li>\n\n\n\n<li>If false, the loop stops<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Example of do 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>    do {<br>        cout &lt;&lt; i &lt;&lt; endl;<br>        i++;<br>    } while (i &lt;= 5);<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: User Input Program<\/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>    do {<br>        cout &lt;&lt; \"Enter a number (0 to stop): \";<br>        cin &gt;&gt; number;<br><br>        cout &lt;&lt; \"You entered: \" &lt;&lt; number &lt;&lt; endl;<br><br>    } while (number != 0);<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between while and do while Loop<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>while Loop<\/th><th>do while Loop<\/th><\/tr><\/thead><tbody><tr><td>Condition checked first<\/td><td>Code executes first<\/td><\/tr><tr><td>May not execute at all<\/td><td>Executes at least once<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example Showing Difference<\/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 = 10;<br><br>    do {<br>        cout &lt;&lt; \"Executed once\";<br>    } while (i &lt; 5);<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Even though the condition is false, the loop runs once.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Infinite do while Loop<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>do {<br>    cout &lt;&lt; \"Infinite Loop\";<br>} while (true);<\/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 do while Loop is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>do while<\/code> loop is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ensures code executes at least once<\/li>\n\n\n\n<li>Is useful for menu-driven programs<\/li>\n\n\n\n<li>Handles user input efficiently<\/li>\n\n\n\n<li>Simplifies repetitive tasks<\/li>\n\n\n\n<li>Supports condition-based repetition<\/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 entering a password:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You must enter it at least once<\/li>\n\n\n\n<li>The system checks if it is correct afterward<\/li>\n\n\n\n<li>If incorrect, it asks again<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is how a <code>do 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>do while<\/code> loop in C++ is a useful looping structure when code needs to execute at least once before checking a condition. It is commonly used in menus, input validation, and interactive applications.<\/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 > do while Loop<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":18,"template":"","class_list":["post-83","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>do while Loop - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ do while loop with syntax, examples, and use cases where code runs at least once before condition checking.\" \/>\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=\"do while Loop - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ do while loop with syntax, examples, and use cases where code runs at least once before condition checking.\" \/>\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:57:40+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=do-while-loop\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"do while Loop - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T05:16:25+00:00\",\"dateModified\":\"2026-05-22T07:57:40+00:00\",\"description\":\"Learn C++ do while loop with syntax, examples, and use cases where code runs at least once before condition checking.\",\"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 > do 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":"do while Loop - Learn C++Language with GiGz.PK","description":"Learn C++ do while loop with syntax, examples, and use cases where code runs at least once before condition checking.","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":"do while Loop - Learn C++Language with GiGz.PK","og_description":"Learn C++ do while loop with syntax, examples, and use cases where code runs at least once before condition checking.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T07:57:40+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=do-while-loop","url":"https:\/\/gigz.pk\/","name":"do while Loop - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T05:16:25+00:00","dateModified":"2026-05-22T07:57:40+00:00","description":"Learn C++ do while loop with syntax, examples, and use cases where code runs at least once before condition checking.","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 > do 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\/83","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=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}