do..while loop in c


do…while Loop

Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied, i.e., loops are used in performing repetitive work in programming.

Suppose you want to execute some code/s 10 times. You can perform it by writing that code/s only one time and repeat the execution 10 times using loop.

do…while loop

In C, do…while loop is very similar to while loop. Only difference between these two loops is that, in while loops, test expression is checked at first but, in do…while loop code is executed at first then the condition is checked. So, the code are executed at least once in do…while loops.

Syntax of do…while loops

do {
   some code/s;
while (test expression);

At first codes inside body of do is executed. Then, the test expression is checked. If it is true, code/s inside body of do are executed again and the process continues until test expression becomes false(zero).

Notice, there is semicolon in the end of while (); in do…while loop.

Example of do…while loop

Write a C program to add all the numbers entered by a user until user enters 0.

/*C program to demonstrate the working of do...while statement*/
#include <stdio.h>
int main(){
   int sum=0,num;
   do             /* Codes inside the body of do...while loops are at least executed once. */
        printf("Enter a number\n");
return 0;


Enter a number
Enter a number
Enter a number

In this C program, user is asked a number and it is added with sum. Then, only the test condition in the do…while loop is checked. If the test condition is true,i.e,num is not equal to 0, the body of do…while loop is again executed until numequals to zero.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s