Open In App

do...while Loop in C

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

C do...while loop is a type of loop that executes a code block until the given condition is satisfied. Unlike the while loop, which checks the condition before executing the loop, the do...while loop checks the condition after executing the code block, ensuring that the code inside the loop is executed at least once, even if the condition is false from the start.

Example:

C
#include <stdio.h>

int main() {
    
    // Loop variable declaration and initialization
    int i = 0;
    
    // do while loop
    do {
        printf("Geeks\n");
        i++;
    } while (i < 3);

    return 0;
}

Output
Geeks
Geeks
Geeks

Explanation: The do...while loop in this C program prints "Geeks" three times by executing the loop body at least once and continuing until the condition i < 3 becomes false.

Syntax

C
do {
    // Body of the loop
    // Update expression
} while (condition);

where,

  • condition: The loop condition which will decide the number of times the loop runs.
  • update expression: The expression that updates the condition to move it towards the loop end in each repetition.

Working of do...while Loop

Let's understand the working of do while loop using the below flowchart.

flowchart of do...while loop in C
Flowchart of do...while Loop in C
  1. When the program control comes to the do...while loop, the body of the loop is executed first and then the test condition/expression is checked, unlike other loops where the test condition is checked first. Due to this property, the do...while loop is also called exit controlled or post-tested loop.
  2. When the test condition is evaluated as true, the program control goes to the start of the loop and the body is executed once more.
  3. The above process repeats till the test condition is true.
  4. When the test condition is evaluated as false, the program controls move on to the next statements after the do...while loop.

The initialization and updation is not a part of the do...while loop syntax. We have to do that explicitly before and in the loop respectively.

Examples of do while Loop

The below programs demonstrate how to use the do while loops in C programs in different situations:

do...while Loop for False Condition

C
#include <stdbool.h>
#include <stdio.h>

int main() {

    // Declaring a false variable
    bool c = false;

    do {
        printf("This is loop body.");
      
      // False condition
    } while (c); 

    return 0;
}

Output
This is loop body.

Explanation: As we can see, the body of the loop is executed even if the condition was false from the start.

We can also nest one do...while loop into another loop (called nested loops). This helps in printing multidimensional structures such as matrices.

C
#include <stdio.h>

int main() {

    // Declaring loop variables
    int i = 0, j;
    int c = 0;

    // Outer loop starts
    do {
        j = 0;

        // inner loop starts
        do {
            printf("%d  ", c++);
            j++;
        } while (j < 3);
        printf("\n");
        i++;
    } while (i < 3);

    return 0;
}

Output
0  1  2  
3  4  5  
6  7  8  

Difference between while and do...while Loop

The following table lists the important differences between the while and do...while Loop .

while Loop

do...while Loop

The test condition is checked before the loop body is executed.The test condition is checked after executing the body.
When the condition is false, the body is not executed not even once.The body of the do...while loop is executed at least once even when the condition is false.
It is a type of pre-tested or entry-controlled loop.It is a type of post-tested or exit-controlled loop.
Semicolon is not required.Semicolon is required at the end.

Next Article
Article Tags :

Similar Reads