View Course Path

Do-while loop in C – Full explanation with examples and tutorials

This one’s simple to understand. The entire concept of a do-while loop lies in its name. The first thing that the command does is that it “does” something, and then it checks for a condition. This means that before a condition is checked for, the contents of the loop execute at least one time. If later, the condition holds true, the loop will execute again. The sequence of events is something as follows:

Execute loop -> Check for condition (if true) -> Execute loop -> Check for condition (if false) -> Exit loop.

do while loop in C flowchart

What is the syntax of the do-while loop in C?

do 
{
   statement(s);
} 
while( condition );

What is a do-while loop? How do these do while loops work? Where should you use a do-while loop?

As explained above a do-while loop executes the set of statements first and then check for the condition.

The control enters the loop normally, executes it and then exits the loop to check the condition. If the condition is false then the control transfers to the subsequent command and the loop is history. However, if the condition is true then the control goes back to the beginning of the loop and its contents execute all over again.

The do-while loop in C is used mainly when you want to execute a set of commands at least once even if the condition is false.

What does the flow chart of a do-while loop in C look like?

do-while-loop-flowchart in C explanation simple examples

What is the difference between a do-while loop and a while loop?

A while loop checks the condition first, if the condition is satisfied, the control enters the loop and executes the statements within the loop. In short, the contents of the loop never execute even once before the condition is checked. Meanwhile, in a do while loop, the loop executes at least once before the condition is checked.

Example of a while loop

#include <stdio.h>
int main()
{
int a=0;
while(a==1)
{
printf("I am in the loop");
}
printf("I am out of the loop");
}

Output

I am out of the loop

Example of a do-while loop

#include <stdio.h>
int main()
{
int a=0;
do
{
printf("I am in the loop");
}
while(a==1)
printf("\nI am out of the loop");
}

Output

I am in the loop
I am out of the loop

What is the difference between a do-while loop, a while loop and a for loop?

  • The main difference between the three is that in for loops and while loops, the condition is checked before the control enters the loop. Whereas in the do-while loop, the condition is checked when the control exits the loop.
  • Another main difference is in the syntax. In for and while loops, there is no semicolon after the condition. However, there is a semicolon after the condition in a do-while loop. This makes sense because if a semicolon were entered after the condition in for and while loop, the control would never enter the loop. As a semicolon signals the end to a command.
  • In a for loop, the initialization of the condition along with updating it forms a part of the syntax. In a while and do-while, it doesn’t.

Example 1: Write a program in C using a do while loop to print something n times.

//print number of times the user wants to print something
#include<stdio.h>
#include<conio.h>
void main()
{
int i, x=0;
printf("Enter number of times you want to print hello\n");
scanf("%d",&i);
do
{
printf("Hello\n");
x++;
}
while(x<i);
getch();
}

Output

Enter number of times you want to print hello
1
Hello

Example 2: Write a program in C using a do while loop to take a number from the user and print the sum of its digits. 

//take number from the user and print sum of digits
#include<stdio.h>
#include<conio.h>
void main()
{
int i,temp,a,sum=0;
printf("Enter number\n");
scanf("%d",&i);
do
{
temp=i%10; //separate the numbers starting from the RHS and store to temp
a=temp; //assign seperated numbers to a
i=i/10; //remove the LSB of the number
sum=sum+a; //add the seperated digits
}
while(i>0);
printf("sum of digits = %d",sum);
getch();
}

Output

Enter number
234
sum of digits = 9

First, a number is taken from the user and stored in a variable. Then we use the modulo function to get the remainder by dividing the number by 10. And then we decrease the number in terms of numerals by dividing it by 10. The separated number is added in every iteration of the loop. The condition remains true as long as the division of the number does not yield 0. This happens only after all the digits are added.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.