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

do…while Loop in C

Last Updated : 24 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Loops in C language are the control flow statements that are used to repeat some part of the code till the given condition is satisfied. The do-while loop is one of the three loop statements in C, the others being while loop and for loop. It is mainly used to traverse arrays, vectors, and other data structures.

What is do…while Loop in C?

The do…while in C is a loop statement used to repeat some part of the code till the given condition is fulfilled. It is a form of an exit-controlled or post-tested loop where the test condition is checked after executing the body of the loop. Due to this, the statements in the do…while loop will always be executed at least once no matter what the condition is.

Syntax of do…while Loop in C

do {
 
    // body of do-while loop    
    
} while (condition);

How to Use do…while Loop in C

The following example demonstrates the use of do…while loop in C programming language.

C




// C Program to demonstrate the use of do...while loop
#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

How does the do…while Loop works?

do while loop syntax

Syntax Structure of do while loop

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

Nested do…while Loop in C

As with other loops, we can also nest one do…while loop into another loop. It is demonstrated using the following C program.

Example of Nested do…while Loop in C:

C




// C Program to demonstrate the nesting of do...while loop
#include <stdio.h>
 
int main()
{
 
    // declaring loop variables
    int i = 0, j;
    int count = 0;
 
    // outer loop starts
    do {
        j = 0;
 
        // inner loop starts
        do {
            printf("%d  ", count++);
            j++;
        } while (j < 3);
        // inner loop ends
 
        printf("\n");
        i++;
    } while (i < 3);
    // outer loop ends
 
    return 0;
}


Output

0  1  2  
3  4  5  
6  7  8  

To know more about nested loops in C, refer to this article – Nested Loops in C with Examples

Examples of do…while Loop in C

Example 1. C Program to demonstrate the behavior of do…while loop if the condition is false from the start.

C




// C Program to demonstrate the do...while loop behaviour
// when the condition is false from the start
#include <stdbool.h>
#include <stdio.h>
 
int main()
{
 
    // declaring a false variable
    bool condition = false;
 
    do {
        printf("This is loop body.");
    } while (condition); // false condition
 
    return 0;
}


Output

This is loop body.

As we can see, even when the condition is false at the start, the loop body is executed once. This is because in the do…while loop, the condition is checked after going through the body so when the control is at the start,

  1. It goes through the loop body.
  2. Executes all the statements in the body.
  3. Checks the condition which turns out to be false.
  4. Then exits the loop.

Example 2. C Program to print Multiplication Table of N using do…while loop

The following example demonstrates the use of do…while loop for printing the multiplication table of N.

C




// C Program to print multiplication table using do...while
// loop
#include <stdio.h>
 
int main()
{
 
    int N = 5, i = 1;
 
    do {
        printf("%d\tx\t%d\t=\t%d\n", N, i, N * i);
    } while (i++ < 10);
 
    return 0;
}


Output

5    x    1    =    5
5    x    2    =    10
5    x    3    =    15
5    x    4    =    20
5    x    5    =    25
5    x    6    =    30
5    x    7    =    35
5    x    8    =    40
5    x    9    =    45
5    x    10    =    50

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.

To know more about these differences, please refer to this article – Difference between while and do-while loop in C, C++, Java

Conclusion

In conclusion, the use of the only exit-controlled loop in C, the do…while loop is also to iterate a particular part of code but the way it works makes it different from entry-controlled loops such as the while loop and the for loop. It is useful in cases where we need to execute the statement inside the loop body at least once such as in traversing circular linked lists.

FAQs on C do…while Loops

1. How many types of loops are there in C?

Ans: There are 3 types of loops in C language:

  1. for Loop
  2. while Loop
  3. do…while Loop

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

Ans: 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.

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

Ans: 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.

4. Which loop is guaranteed to execute at least once?

Ans: 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.

5. Can we skip braces in C do…while loop syntax if there is only one statement in the body?

Ans: No, we cannot skip braces in C do…while syntax even if there is only a single statement unlike while and for loop.

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

Ans: We can create an infinite loop in C by specifying a condition that will always be true as the loop condition. The below program demonstrates how to do that:

C




// C Program to create a infinite loop using do...while loop
// in C
#include <stdio.h>
 
int main()
{
    // infinite loop
    do {
        printf("gfg ");
    } while (1); // always 1 ~ true
 
    return 0;
}


Output

gfg gfg gfg gfg gfg gfg gfg .... (infinite)

Related Articles:



Previous Article
Next Article

Similar Reads

Difference between while and do-while loop in C, C++, Java
while loop: A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. Syntax : while (boolean condition) { loop statements... } Flowchart: Example: C/C++ Code #include &lt;stdio.h&gt; int main() { int i = 5; while (i &lt; 10)
2 min read
Which will be faster while(1) or while(2)?
In most Programming Languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false. while(1) & while(2) It is an infinite loop that will run till a break statement is issued explicitly. Neither while(1) nor while(2) but any integer whic
2 min read
Difference between while(1) and while(0) in C language
Prerequisite: while loop in C/C++ In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false. while(1) It is an infinite loop which will run till a break statement is issued explicitly. Interestingly not
3 min read
while loop in C
The while Loop is an entry-controlled loop in C programming language. This loop can be used to iterate a part of code while the given condition remains true. Syntax The while loop syntax is as follows: while (test expression) { // body consisting of multiple statements }Example The below example shows how to use a while loop in a C program C/C++ Co
3 min read
Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
For Versus While
Question: Is there any example for which the following two loops will not work the same way? [GFGTABS] C /*Program 1 --&gt; For loop*/ for (&lt;init - stmnt&gt;;&lt;boolean - expr&gt;;&lt;incr - stmnt&gt;) { &lt;body-statements&gt; } /*Program 2 --&gt; While loop*/ &lt;init - stmnt&gt;; while (&lt;boolean - expr&gt;) { &lt;body-statements&gt; &lt;i
1 min read
A nested loop puzzle
Which of the following two code segments is faster? Assume that compiler makes no optimizations. C/C++ Code /* FIRST */ for(i = 0; i &lt; 10; i++) for(j = 0; j &lt; 100; j++) //do something // This code is contributed by Shubham Singh C/C++ Code /* FIRST */ for(i=0;i&lt;10;i++) for(j=0;j&lt;100;j++) //do something Java Code /* First */ for(i = 0; i
7 min read
Print 1 to 100 without loop using Goto and Recursive-main
Our task is to print all numbers from 1 to 100 without using a loop. There are many ways to print numbers from 1 to 100 without using a loop. Two of them are the goto statement and the recursive main. Print numbers from 1 to 100 Using Goto statement Follow the steps mentioned below to implement the goto statement: declare variable i of value 0decla
5 min read
Why are elementwise additions much faster in separate loops than in a combined loop?
When adding more than 2 arrays, separate loops for adding elements step by step prove to give a better performance than adding the elements in a single loop. To Test the above statement, two code blocks add the 4 array elements namely array a with b and c with d. The first code block uses a combined loop to add them while the second block uses two
3 min read
Difference between Sentinel and Counter Controlled Loop in C
Sentinel Controlled Loop A sentinel controlled loop is also called an indefinite repetition loop because the number of iterations is not known before the loop starts executing. In a sentinel controlled loop, a special value called sentinel value is used to change the loop control expression from true to false in order to determine whether to execut
3 min read
Article Tags :
three90RightbarBannerImg