The do-while loop in c in an exit controlled loop unlike the for and the while loop the condition is checked at the end of the loop(i.e, after the execution of all the statements inside the do-while block), therefore the do-while loop runs at least once in the program even if the condition fails.
do
{
//statements to be executed
}while(condtion);
The do-while loop in c is used in cases where we need to ensure that the statements within the loop are executed at least once.
#include <stdio.h>
int main() {
do
{
printf("coderzpy.com");
}while(4<1);
return 0;
}
coderzpy.com
In the above example, even though the condition is false, we get the output as coderzpy.com once.
Note: break and continue statements are used with the do-while as well as the while loop. We shall see further about them in the next post.
The do..while loop can also be used to create the infinite loop. The following is the syntax to create the infinite do..while loop.
do
{
// body of the loop..
}while(1);
As we can see the condition will always remain true in the above code, therefore it is an infinite loop.
Note: also read about while loop in c & while loop examples in c
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…