Do While Loop in C

Do While Loop in C Language with Examples

In this article, I am going to discuss the Do While loop in C Language with Examples. Please read our previous articles, where we discussed While loop in C Language with Examples. At the end of this article, you will understand what is the do-while loop and when and how to use a do-while loop in the C Program with examples.

Do while loop in C Language:

The do-while loop is a post-tested loop. Using the do-while loop, we can repeat the execution of several parts of the statements. The do-while loop is mainly used in the case where we need to execute the loop at least once. The do-while loop is mostly used in menu-driven programs where the termination condition depends upon the end-user.

Do While Loop in C Language with Examples

Syntax to use Do While Loop in C:

Do While Loop Syntax in C Language

Program to understand do while loop in c:
#include <stdio.h>
int main()
{
   int j=0;
   do
   {
       printf("Value of variable j is: %d\n", j);
       j++;
   }while (j<=3);
   return 0;
}
Output:

Do While Loop in C Language

Note: When you want to execute the loop body at least once irrespective of the condition, then you need to use the do-while loop.

Nested do-while Loop in C Language:

Using a do-while loop within do-while loops is said to be a nested do-while loop. The syntax to use the nested do-while loop in C language is given below.

Nested do-while Loop in C Language

 

Program to Understand Nested do-while Loop in C Language:
#include <stdio.h>
int main ()
{
    do
    {
        printf ("I'm from outer do-while loop ");
        do
        {
            printf ("\nI'm from inner do-while loop ");
        }
        while (1 > 10);
    }
    while (2 > 10);
    return 0;
}
Output:

Program to Understand Nested do-while Loop in C Language

In the next article, I am going to discuss For Loop in C Language with examples. Here, in this article, I try to explain Do While Loop in C Language with examples. I hope you enjoy this Do While Loop in C Language with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.