21
NovUnderstanding do...while loop in C
do...while Loop in C: An Overview
The do-while loop is similar to the while loop. But this loop will execute the code block once, before checking if the condition is true. Afterward, this loop will repeat itself as long as the condition is true.
In this C Tutorial, we will explore more about the do...while loop which will include what is do...while in C, do...while loop in c programming with example, and the syntax of do...while loop in C.
Read More - Top 50 C Interview Questions and Answers
Types of Loop in C
Let’s get into the three types of loops used in C programming.for
loopwhile
loopdo while
loop
But right now we are going to focus on only Do while Loop, So let's discuss it.
What is a do...while loop?
Do While is an exit-controlled loop. It prints the output at least once before checking the condition. Afterwards, the condition is checked and the execution of the loop begins.
Flowchart :
Syntax
do{
//code to be executed
}while(test condition);
- The body of the loop executes before checking the condition.
- If the
test condition
istrue
, the loop body executes again. - Again the
test condition
is evaluated. - The process repeats until the
test condition
becomesfalse
.
Example: do...while loop in C
#include <stdio.h>
int main()
{
int i = 0;
do {
printf("%d\n", i+1);
i++;
}
while (i < 10);
return 0;
}
- The above code prints numbers from 1 to 10 using the
do while
loop in C. - It prints 1 before checking if
i
less than 10. - It checks the condition
i<10
and executes untili
becomes 10
Output
1
2
3
4
5
6
7
8
9
10
Conclusion:
FAQs
- The structure of a do-while loop is simple and easy to understand anyone.
- The code inside a do-while loop is always executed at least once in life, regardless of the condition.
- The code inside a do-while loop will continue to execute until the condition is True.