Like while loop in C, the do-while loop also executes the statement or body of statements several times in programming. Actually, there are different results and logic to be applied. If the conditional expression in the while loop is initially false, then the body of the loop will not get executed at all. However, sometimes it is needed to execute the body of a while loop at least once, even if the condition is false to start.
Basically do-while loop is totally reverse while loop as per as execution is concerned. It is also called an exit-controlled loop. In the case of a while loop, the conditional expression is checked at the beginning. If the condition in the do-while loop is true then the statements are executed or else it will exit. So, it keeps on doing execution as long as the condition is true.
But in the do-while loop case, all the statements in the do-block are executed sequentially. After full execution, the conditional expression is checked. If it is true, then the control keeps on doing execution. Most importantly, in the do-while loop, the while condition is written at the end and it terminates with a semicolon (;) while in while loop it is not required.
Syntax of Do-While Loop in C
do
{
//body of loop
} while(condition);
Each iteration of do-while loop, the body of do statement is executed first, then condition in the while is checked at the end. As a result of which, the body of do statement is executed even if the condition is false.
Example: tick program that demonstrates the do-while loop
#include<stdio.h>
int main()
{
int n=10;
do {
n--;
printf("Tick :",n);
printf("\t");
} while(n>0);
}
Output:
9,8,7,6,5,4,3,2,1,0
Explanation: Here in the diagram, the value of n will be printed decreasingly until n becomes 0. In the body of do-block, first the initial value of n will be decremented after that value of n will be printed. The control will come out of do-block and will check the condition in while. But in while loop case, if you put the same code, the output will be printed from 9 up to 1 because first the condition is checked if it comes true, then the statements inside body will be executed as long as the condition is true.
Do-while loop with the break statement
Generally, the job of break statement is to halt the program in a body and the control comes out of the body to execute the remaining statements. Same logic of break statement is also applied in do-while loop also. When the programmer wants to execute certain number of codes and stop execution somewhere in block even if the condition is false.
Example: given number is 10 only 5 numbers to be printed decreasingly
#include<stdio.h>
int main()
{
int n=10;
do {
printf("value of n: %d",n);
printf("\t");
n--;
if(n==5)
{
break;
}
else
{
continue;
}
} while(n>0);
}
Output:
The value of n:10
The value of n:9
The value of n:8
The value of n:7
The value of n:6
Explanation: Here, the user wants to print only 5 numbers out of 10 number decreasingly. The control goes on executing the statements until the value of n is 5. When the control meets the if condition to be true. The break statement gets executed and control comes out of the block without checking the while condition.
Advantages of Do-While Loop in C
The do-while loop is useful when you process menu selection because you will usually want the body of the menu loop to execute at least once.
#include<stdio.h>
int main()
{
char choice;
do
{
printf("Help on");
printf("1.apple");
printf("2.banana");
printf("3.jack fruit");
printf("4.papaya");
printf("5.orange");
printf("choose one");
scanf("%c",choice);
} while(choice<'1'||choice>'5');
printf("\\n");
switch(choice)
{
case '1':
printf("Apple is farmed in kashmir");
break;
case '2':
printf("banana is farmed in everywhere");
break;
case '3':
printf("jack fruit is farmed in odisha");
break;
case '4':
printf("papaya is farmed in AP");
break;
case '5':
printf("orange is farmed in J&K");
break;
}
}
Output:
Help on:
1. apple
2. banana
3. jackfruit
4. papaya
5. orange
Choose one:
4
Papaya is farmed in AP.
Explanation: Here, the do-while loop is implemented to verify that the user has entered a valid choice. If not, then the user is repromoted. Since the menu must be displayed at least once.
This Article is written by Pitabash Choudhury. He is an MCA student.