Do While Loop in C - Syntax and Examples
do... while loop
Do while loop in C is used when we want to execute the statements inside loop at least once. More deep explanation is given below.Syntax:
do
{
statement 1;
statement 2;
statement n;
}while(condition);
Explanation / How it works:
When using do while loop first the statements inside do... while block are executed and then the condition is checked. If the condition is true for the next iteration then the statements are executed once again. The statement(s) enclosed in curly braces get executed until the condition evaluates to false. There is no need to use the curly braces ({}
) if the body of do while loop contains only one statement. Note that we need to place the semicolon (;
) at end of condition.
Key Points To Remember:
- This loop will iterate at least once even if the condition is false initially.
- It is also called as exit controlled or bottom tested as the condition is checked at bottom, after iterating the statement(s).
do while loop example:
Write a C program to reverse a number and also display the original number./* C program to reverse a number */
#include<stdio.h>
int main()
{
int number,remainder,reverse=0,original;
printf("\n Enter any number to reverse:- ");
scanf("%d",&number); // Accepting a number,storing it into 'number' variable
original=number; // Storing the original number into 'original' variable
do{
remainder=number%10; // Getting last digit of user input
reverse=reverse*10+remainder; // Calculating the reverse number
number=number/10; /* Dividing number by 10 so that we get only new number excluding the last digit. For Example: number=1234 by performing number=1234/10; we get number=123 as number is of integer data type */
}while(number>0); // Iterating the loop while number>0
printf("\n %d is the reverse number of %d \n",reverse,original); // Displaying the original and reverse number
return 0;
}
/* Output of above code:-
Enter any number to reverse:- 1234
4321 is the reverse number of 1234
*/
Explanation of above code:
Most of the the code is explained in comment but still we will see how do while loop works.Suppose user entered 1234, we stored it in 'number' as well as in 'original' variable.
For the first iteration, we have number=1234
- remainder=number%10;
i.e remainder=1234%10; i.e remainder=4 - Another statement is
reverse=reverse*10+remainder;
i.e reverse=0*10+4 as reverse is initialized to 0. - Next is
number=number/10;
i.e number=1234/10; i.e number=123 (It stores 123 instead of 123.4 because number is of type integer and not float)
Now second iteration,
- The first statement ,
remainder=number%10; i.e remainder=123%10; (as values of number is now 123) i.e remainder=3; - Next is
reverse=reverse*10+remainder;
i.e reverse=4*10+3; (as reverse=4 from first iteration) - Next is
number=number/10;
i.e number=123/10;
i.e number=12;
- The first statement ,
remainder=number%10; i.e remainder=123%10; (as values of number is now 123) i.e remainder=3; - Next is
reverse=reverse*10+remainder;
i.e reverse=4*10+3; (as reverse=4 from first iteration) - Next is
number=number/10;
i.e number=123/10;
i.e number=12;
In third iteration,
remainder=2;
reverse=432;
number=1;
After third iteration, again the condition is true and control moves to beginning of the do while loop.
For fourth iteration,
remainder=1;
reverse=4321;
number=0; (as 1/10 results in 0, considering only the integer part)
After fourth iteration, the condition in while becomes false and control moves to the first statement outside the loop. And therefore we print the original and reverse of a given number.
Difference between while loop and do... while loop
while loop | do while loop |
---|---|
while loop is top tested. | do while loop is bottom tested |
while loop do not execute, not at least once if the condition evaluates to false. | do while loop executes at least once even if the condition is false. |
|
|