c
C Programming Tutorial : Introduction

C Loops: Do While with Example

The do-while loop is also used for looping. The body of this loop may contain a single statement or a block of statements. The syntax for writing this loop is:

Syntax -1

do 
    statement;
while(condition);
 

Syntax -2

do{
    statement;
    statement;
    …………….
    …………….
  }while (testExpression);
 
do-while-loop flow chart

Here firstly the statements inside loop body are executed and then the condition is evaluated. If the condition is true, then again, the loop body is executed, and this process continues until the condition becomes false. Note that unlike while loop, here a semicolon is placed after the condition.

In a while loop, first the condition is evaluated and then the statements are executed whereas in a do while loop, first the statements are executed and then the condition is evaluated. So, if initially the condition is false the while loop will not execute at all, whereas the do while loop will always be executed at least once.

Example-1

/*Program to print the numbers from 1 to 10 using do... while loop*/

#include<stdio.h>
#include<conio.h>

void main ()
{
    int i=1;
    do
        {
            printf("%d\t",i);
            i=i+1;
        }while(i<=10);
    printf("\n");
    getch();
}

Output:
1 2 3 4 5 6 7 8 9 10
 

Generally, while loop is used more frequently than the do while loop, but some situations may arise when it is better to check the condition at the bottom of loop. For example, in the next two programs it better to use a do while loop.

Example-2

/* Program to count the digits in any number */
#include<stdio.h> 
#include<conio.h> 

void main ( )
{
    int n, count=0, rem;
    printf ("Enter the number ");
    scanf ("%d”,&n) ;
    do
        {
            n/=10;
            count++;
        }while(n>0) ;
    printf("Number of digits = %d\n”, count);
    getch();
}
 

If we write the same program using a while loop, then we will not get correct answer when input number Is 0. The count of digits in number 0, it is 1 but using while loop the answer will come out to be zero.

Example-3

/*Program to find the sum of numbers entered*/
#include<stdio.h>
#include<conio.h>

void main()
{
    int num, sum=0;
    do
        {
            printf ("Enter a number<0 to stop”);
            scanf("%d",&num);
            sum+=num;
        } while(num! =0);
    printf ("Sum is %d\n", sum);
    getch();
}
 

In this program if we use while loop then the condition will be checked at the top, so either we will have to give some initial value to the variable num or we will have to write duplicate printf() and scanf( ) statements before the loop.

The do… while loop is generally used for checking validity of entered data. Suppose we want the user to enter an employee ID, and the valid IDs are in the range 100 to 500 only. If the user enters an invalid ID, we want to ignore that ID and again ask him to enter another one, and we want this process to continue till he enters a valid ID. In this type of situation, we can use do … while loop.

 Do{
    printf("Enter employee ID ");
    scanf("%d",&emp_id) ;
   }while (emp_id<100 I I emp_id>50);
 

This loop will terminate only when the entered number is in the valid range 100 to 500.