{"id":2291,"date":"2024-05-10T07:19:25","date_gmt":"2024-05-10T07:19:25","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2291"},"modified":"2024-05-10T07:19:25","modified_gmt":"2024-05-10T07:19:25","slug":"do-while-loop-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/do-while-loop-in-c\/","title":{"rendered":"Do\u2026While Loop In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#do-while-loop-in-c\">Do\u2026While Loop In C<\/a><\/li><li ><a href=\"#what-is-do-while-loop-in-c\">What is do\u2026while Loop in C?<\/a><\/li><li ><a href=\"#syntax-of-do-while-loop-in-c\">Syntax of do\u2026while Loop in C<\/a><\/li><li ><a href=\"#how-to-use-do-while-loop-in-c\">How to Use do\u2026while Loop in C<\/a><\/li><li ><a href=\"#nested-do-while-loop-in-c\">Nested do\u2026while Loop in C<\/a><\/li><li ><a href=\"#examples-of-do-while-loop-in-c\">Examples of Do\u2026While Loop in C<\/a><\/li><li ><a href=\"#key-difference-between-while-and-do-while-loop-in-c\">Key Difference between while and do\u2026while Loop in C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"do-while-loop-in-c\">Do\u2026While Loop In C<\/h2>\n\n\n\n<p>Loops in C are control flow constructs that enable the repetition of a specific block of code until a particular condition is met. The do-while loop, along with the while loop and for loop, is one of the three primary loop constructs in C.<\/p>\n\n\n\n<p>The do-while loop is particularly useful when you need to execute a block of code at least once, even if the condition is not initially met. It is commonly used for tasks like traversing arrays, vectors, and other data structures where you want to ensure that the loop body runs at least once before checking the loop condition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-do-while-loop-in-c\">What is do\u2026while Loop in C?<\/h2>\n\n\n\n<p>The&nbsp;do\u2026while in C&nbsp;is a loop statement used to repeat some part of the code till the given condition is fulfilled. It is a form of an&nbsp;exit-controlled or post-tested loop&nbsp;where the test condition is checked after executing the body of the loop. Due to this, the statements in the do\u2026while loop will always be executed at least once no matter what the condition is.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-do-while-loop-in-c\">Syntax of do\u2026while Loop in C<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>do {\n \n    \/\/ body of do-while loop    \n    \n} while (condition);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-do-while-loop-in-c\">How to Use do\u2026while Loop in C<\/h2>\n\n\n\n<p>The example below shows  the do-while Loop in C Program<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate the use of do...while loop\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ loop variable declaration and initialization\n    int i = 0;\n    \/\/ do while loop\n    do {\n        printf(\"Skill Vertex\\n\");\n        i++;\n    } while (i &lt; 3);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skill Vertex \nSkill Vertex \nSkill Vertex<\/code><\/pre>\n\n\n\n<p>The do-while loop in C is unique because it always runs its code block at least once. Here&#8217;s how it works:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First, it executes the code inside the loop.<\/li>\n\n\n\n<li>Then, it checks a condition.<\/li>\n\n\n\n<li>If the condition is true, it repeats the process.<\/li>\n\n\n\n<li>If the condition is false, it moves on to the next part of the program.<\/li>\n<\/ol>\n\n\n\n<p>This loop is sometimes called an &#8220;exit-controlled&#8221; loop because it checks the exit condition after running the code block. It&#8217;s handy when you want to ensure that certain code runs at least once before checking if it should repeat.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"nested-do-while-loop-in-c\">Nested do\u2026while Loop in C<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate the nesting of do...while loop\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ declaring loop variables\n    int i = 0, j;\n    int count = 0;\n \n    \/\/ outer loop starts\n    do {\n        j = 0;\n \n        \/\/ inner loop starts\n        do {\n            printf(\"%d  \", count++);\n            j++;\n        } while (j &lt; 3);\n        \/\/ inner loop ends\n \n        printf(\"\\n\");\n        i++;\n    } while (i &lt; 3);\n    \/\/ outer loop ends\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0  1  2  \n3  4  5  \n6  7  8  <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"examples-of-do-while-loop-in-c\">Examples of Do\u2026While Loop in C<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Example 1. C Program to check the behavior of do\u2026while loop if the condition is false from the start.<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to demonstrate the do...while loop behaviour\n\/\/ when the condition is false from the start\n#include &lt;stdbool.h&gt;\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    \/\/ declaring a false variable\n    bool condition = false;\n \n    do {\n        printf(\"This is loop body.\");\n    } while (condition); \/\/ false condition\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is loop body.<\/code><\/pre>\n\n\n\n<p>The do\u2026while loop operates in C, states the fact that the loop body is executed at least once, even when the condition is initially false. This is due to the specific behavior of the do\u2026while loop, where the condition is checked after the first execution of the loop body.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example 2. C Program to print Multiplication Table of N using do\u2026while loop<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to print multiplication table using do...while\n\/\/ loop\n#include &lt;stdio.h&gt;\n \nint main()\n{\n \n    int N = 5, i = 1;\n \n    do {\n        printf(\"%d\\tx\\t%d\\t=\\t%d\\n\", N, i, N * i);\n    } while (i++ &lt; 10);\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5    x    1    =    5\n5    x    2    =    10\n5    x    3    =    15\n5    x    4    =    20\n5    x    5    =    25\n5    x    6    =    30\n5    x    7    =    35\n5    x    8    =    40\n5    x    9    =    45\n5    x    10    =    50<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"key-difference-between-while-and-do-while-loop-in-c\">Key Difference between while and do\u2026while Loop in C<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>while Loop<\/th><th>do\u2026while Loop<\/th><\/tr><\/thead><tbody><tr><td>The test condition will be checked&nbsp;before the loop body is executed.<\/td><td>The test condition will be checked&nbsp;after executing the body.<\/td><\/tr><tr><td>When the condition is false, the&nbsp;body won&#8217;t be executed not even once.<\/td><td>The body of the&nbsp;<strong>Do\u2026while loop<\/strong> will be executed at least once&nbsp;even though the condition turns out to be false.<\/td><\/tr><tr><td><strong>Do &#8230; While Loop<\/strong> is a pre-tested or entry-controlled loop <\/td><td><strong>Do &#8230; While Loop<\/strong> is a pre-tested or entry-controlled loop <\/td><\/tr><tr><td>Semicolon is not required.<\/td><td>A semicolon is not necessary.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-do-while-loop-in-c\">FAQ- Do\u2026While Loop In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1696413457823\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What does a do-while loop do in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The do-while statement allows you to repeatedly execute a statement or a block of statements until a specified expression evaluates to false.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696413463615\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the do while looping statement?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A do-while loop is indeed a control flow statement that guarantees the execution of a block of code at least once. After the initial execution, it repeatedly re-executes the block based on a specified boolean condition at the end of the block.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1696413473527\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. When to use while and do-while loop in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In a &#8220;while&#8221; loop, the condition is checked first, and if it is true, the statement(s) are executed. If the condition is false initially, the statement(s) may not execute at all.<br \/>In a &#8220;do-while&#8221; loop, the statement(s) are executed at least once, regardless of the initial condition. After the first execution, the condition is checked, and if it is true, the loop continues to execute.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><br><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Do\u2026While Loop In C Loops in C are control flow constructs that enable the repetition of a specific block of code until a particular condition is met. The do-while loop, along with the while loop and for loop, is one of the three primary loop constructs in C. The do-while loop is particularly useful when &#8230; <a title=\"Do\u2026While Loop In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/do-while-loop-in-c\/\" aria-label=\"More on Do\u2026While Loop In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5344,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[411],"class_list":["post-2291","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-dowhile-loop-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2291"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2291\/revisions"}],"predecessor-version":[{"id":10647,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2291\/revisions\/10647"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5344"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}