Do While Loop in C Programming

In this post, we will learn how to use do while loop in C programming with examples.

To understand the do while loop, you need to know about for loop and the while loop. So if you have not read the tutorials before this, then definitely read them first.

Do while loop is also used like for loop and while loop to repeat some statements in your programs more than once with the same or some changes, but do while loop works in a slightly different way than for loop and while loop does.

Both the for loop and the while loop are entry-controlled loops i.e. The loop starts only after the condition is checked and the statements written in the body of the loops are run only when the condition is true.

The do-while loop exit is a controlled loop i.e. The condition is checked in this loop after the end of the body of the loop as you can see in the syntax below.

You can also read: While Loop In C Programming

do while loop syntax:

do
{
 Statements;
 ------
 ------
 ------
 Update;
<div><div>}while(condition);</div></div>

Do While Loop in C Programming (Note):

Note: Forget to put a semicolon ( ; ) in the last of the while.

Do While Loop in C Programming: Flow Chart

Do While Loop in C Programming
Do While Loop in C Programming

When you need a loop in your programs that first run the statements once and then check the condition, then a do-while loop will be the best choice for such work.

To say the same thing in other words, whenever you want such a loop, even if the condition is false, run the statements at least once, then the do-while loop will be the best choice for such work.

do while loop example program 1:

#include <stdio.h>
int main()
{
    int i=10;
 
    do
    {
        printf("%d ",i);
        i++;
    }while(i<=2);
 
    return 0;
}<gwmw style="display:none;">

Output:

10

Explanation:

As you are seeing in the above example that first the value (10) of variable i was printed and after that the condition was checked and became false and our statement was run only once. 

do while loop example program 2:

In this program, as long as the user continues to press 1, both the statements written in the body of the do will continue to run.

#include <stdio.h>
int main()
{
    int ch;
 
    do
    {
 
        printf("Edutech IT Education\n");
        printf("For Print Again, Press 1 and other number for No");
        scanf("%d",&ch);
 
    }while(ch==1);
 
     printf("This is Outside do-While");
 
    return 0;
}

Output:

Edutech IT Education
For Print Again, Press 1 and other number for No : 1

Edutech IT Education
For Print Again, Press 1 and other number for No: 1

Edutech IT Education
For Print Again, Press 1 and other number for No: 5
This is Outside do-While

Conclusion:

I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at praveen171086@gmail.com.

You can also visit my Hindi YouTube Channel.

Leave a Comment