Avinash Singh Programming,C Programming Do While Loop in C Programming

Do While Loop in C Programming

Do While Loop in C Programming post thumbnail image

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 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.

mm
Avinash Singh

I am Avinash Singh, a software engineer from the profession and have been working in the IT industry since 2009. I live in Barabanki district of Uttar Pradesh in India and completed intermediate in 2001 from Ram Sevak Inter College, a UP Board school. After inter I completed my professional degree (BCA & MCA) from Indira Gandhi National Open University, Delhi in 2008 and along with it I did many certifications to keep my knowledge up to date and the next year I started a software development company E – Vision Technocraft Pvt Ltd.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post