The Wayback Machine - https://web.archive.org/web/20250110105505/https://www.geeksforgeeks.org/c-do-while-loop/
Open In App

do…while Loop in C

Last Updated : 16 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The do…while loop is a type of loop in C that executes a block of code until the given condition is satisfied. The feature of do while loops is that unlike the while loop, which checks the condition before executing the loop, the do…while loop ensures that the code inside the loop is executed at least once, even if the condition is false from the start.

Let’s take a look at an 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 of do while Loop

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

How does the do…while Loop works?

The working of the do…while loop is explained below:

  1. When the program control first 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.

As with the while loop in C, 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.

The flowchart below shows the visual representation of the flow of the do…while loop in C.

C do…while Loop Flowchart

flowchart of do...while loop in C

Flowchart of do…while Loop in C

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.

Print Matrix of 3 x 3

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 in C

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

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.

do…while Loop in C – FAQs

What are the entry-controlled or pre-tested loops?

The entry-controlled loops or pre-tested loops are those loops in which the loop condition is checked before executing the body of the loop.

What are the exit-controlled or post-tested loops?

The exit-controlled loops or post-tested loops are those loops in which the program control comes to the body of the loop before checking the loop condition.

Which loop is guaranteed to execute at least once?

The do…while loop is guaranteed to execute the statements in the body of the loop at least once as it is a type of exit-controlled loop.

How to create an infinite loop in C using the do…while loop?

We can create an infinite loop in C by specifying a condition that will always be true as the loop condition.



Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg