Do-While Loop in C programming language – Iteration Statements

The Do-while loop is an exit controlled loop. In simple words, the test condition is present after the body of the loop.The special feature of a do-while loop is that it executes the body of the loop at least once even if the condition is false.A do-while loop is very similar to a while loop except that it is an exit controlled loop. Here is the syntax of a do-while loop:

initialization;
do
{
  //body of the loop
  update expression;
}while(test condition);

In the syntax, we can clearly see that once all the statements inside the loop are executed the control flows to the while test condition. Have a look at the do-while loop flow chart for better understanding:

do-while-loop

A very similar program to the one seen in for loop and  while loop in the previous tutorials using do-while loop:

#include <stdio.h>
int main()
{
    int i=1;
    do
    {
        printf("CodinGeek\n");
        i++;
    }while(i==0);
    return 0;
}

Output:-
CodinGeek

The first thing we see here is that the while test condition is false. But the body still gets executed and prints CodinGeek once.

Different Types of do-while loop

We already saw the classification in for loop and while loop. Do-while loop also has the following types:

  • Infinite do-while loop: A do-while loop that has an infinite number of iterations is known as an infinite do-while loop. The test condition of the do-while loop never turns false. Here is one example:
int i=1;
 do
 {
   printf("CodinGeek\n");
 }while(i<=5);

This program prints CodinGeek infinite number of times because the while test condition never turns false. If we leave the while test condition blank, we get a compilation error.

  • Empty do-while loop: An empty do-while loop is the one that does not have anything in it’s body. Here’s an example:
int i=1;
 do{
}while(++i<=5);

This program does not give any output as we see that there are no statements inside the loop body. The counter updates itself in the while test condition.

  • Nested do-while loop: As we saw earlier a nested loop is the one which has one or more loops inside its body.A nested do-while loop also has one or more than one loop inside its body. Here is an example:
#include <stdio.h>
int main()
{
    int i=1,j;
	do{
		j=1;
		do{
			printf("CodinGeek\n");
		}while(++j<=2);
		
		printf("-----\n");

	}while(++i<=3);
    
	return 0;
}
Output:-
CodinGeek
CodinGeek
-----
CodinGeek
CodinGeek
-----
CodinGeek
CodinGeek
-----

This loop works in a very similar manner to the while loop. Also the initialization of the inner loop should be inside the body of the outer loop.

For Loop vs Do-while Loop

  • For loop is an entry controlled loop, that is, the test condition is present before the body of the loop. Whereas, do-while loop is an exit controlled loop, that is, the test condition is present after the body of the loop.
  • For loop and do-while loop differ a lot in its syntax. The three parts of the loop are placed at different positions of the loop.
  • For loop will terminate immediately if the test condition is false. Whereas, do-while loop will execute at least once even if the condition is false.
    Read more – For loops in C Language

While Loop vs Do-while Loop

  • While loop is an entry controlled loop, that is, the test condition is present before the body of the loop. Whereas, do-while loop is an exit controlled loop, that is, the test condition is present after the body of the loop.
  • While loop will terminate immediately if the test condition is false. Whereas, do-while loop will execute at least once even if the condition is false.
    Read more – while loops in C Language

So that’s all for this tutorial. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Do come back for more because learning paves way for a better understanding.

Keep Coding!! Happy Coding!! 🙂