C language loops | do while loop

For Loop      While Loop      Do while Loop

Do while loop

syntax:

do
{
statements;
statements;
statements;
}while(condition);

* Do while loop is an exit control loop.
* In the do-while loop the statements of the loop are executed first and then the condition is checked.
* In the do-while loop irrespective of whether the condition is true or false the statements of the loop are executed at least once.

Difference between while loop and do while loop

while loopdo while loop
While(condition)
{
statements;
}
do
{
statements;
}while(condition);
While loop is an entry control loopDo while loop is an exit control loop
In while loop the condition is checked first then the statements of the loop are executedIn do while loop the statements of the loop are executed first and then the condition is checked.
In while loop if the condition is true then only the control will enter the loop and the statements will be executed. In do while loop irrespective of whether the condition is true or false the statements of the loop will get executed at least once.
i=11;
while(i<=10)
{
printf(“%d\n”,i);
i++;
}
o/p : (blank)
i=11;
do
{
printf(“%d\n”,i);
i++;
}while(i<=10);
o/p:11

Examples of do while loop

Question:1
C Program to input for a number, calculate and print its square and add the continue condition?
Sol:

#include <stdio.h>

int main()
{
   int a,b;
   char ch;
   do
   {
       printf("Enter any no ");
       scanf("%d",&a);
       b=a*a;
       printf("Square = %d\n",b);

       printf("Like to cont ... (y/n) ");
       fflush(stdin);
       scanf("%c",&ch);
   }while(ch=='y' || ch=='Y');

    return 0;
}

Output:

Enter any no 2
Square = 4
Like to cont … (y/n) y
Enter any no 6
Square = 36
Like to cont … (y/n) y
Enter any no 5
Square = 25
Like to cont … (y/n) n

Question:2
C Program to input for a number, calculate and print its cube and add the continue condition?
Sol:

#include <stdio.h>

int main()
{
   int a,b;
   char ch;
   do
   {
       printf("Enter any no ");
       scanf("%d",&a);
       b=a*a*a;
       printf("Cube = %d\n",b);

       printf("Like to cont ... (y/n) ");
       fflush(stdin);
       scanf("%c",&ch);
   }while(ch=='y' || ch=='Y');

    return 0;
}

Output:

Enter any no 2
Cube = 8
Like to cont … (y/n) y
Enter any no 5
Cube = 125
Like to cont … (y/n) y
Enter any no 8
Cube = 512
Like to cont … (y/n) n

Question:3
C Program to input for 2 numbers calculate and print their sum and add the continue condition?
Sol:

#include <stdio.h>

int main()
{
   int a,b,c;
   char ch;
   do
   {
       printf("Enter 2 nos ");
       scanf("%d %d",&a,&b);
       c=a+b;
       printf("Sum = %d\n",c);

       printf("Like to cont ... (y/n) ");
       fflush(stdin);
       scanf("%c",&ch);
   }while(ch=='y' || ch=='Y');

    return 0;
}

Output:

Enter 2 nos 10
25
Sum = 35
Like to cont … (y/n) y
Enter 2 nos 23
63
Sum = 86
Like to cont … (y/n) n

Question:4
C Program to input for 3 numbers calculate and print their product and add the continue condition?
Sol:

#include <stdio.h>

int main()
{
   int a,b,c,d;
   char ch;
   do
   {
       printf("Enter 3 nos ");
       scanf("%d %d %d",&a,&b,&c);
       d=a*b*c;
       printf("Product = %d\n",d);

       printf("Like to cont ... (y/n) ");
       fflush(stdin);
       scanf("%c",&ch);
   }while(ch=='y' || ch=='Y');

    return 0;
}

Output:

Enter 3 nos 10
20
30
Product = 6000
Like to cont … (y/n) y
Enter 3 nos 2
6
3
Product = 36
Like to cont … (y/n) y
Enter 3 nos 5
6
2
Product = 60
Like to cont … (y/n) n

Question:5
C Program to take input for 3 numbers, check and print the largest number. Add the continue condition?
Sol:

#include <stdio.h>

int main()
{
   int a,b,c,m;
   char ch;
   do
   {
       printf("Enter 3 nos ");
       scanf("%d %d %d",&a,&b,&c);
       if(a>b && a>c)
        m=a;
       else
        if(b>c)
        m=b;
       else
        m=c;
       printf("Max no = %d\n",m);

       printf("Like to cont ... (y/n) ");
       fflush(stdin);
       scanf("%c",&ch);
   }while(ch=='y' || ch=='Y');

    return 0;
}

Output:

Enter 3 nos 25
96
3
Max no = 96
Like to cont … (y/n) y
Enter 3 nos 25
87
54
Max no = 87
Like to cont … (y/n) n

For Loop      While Loop      Do while Loop

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects