Do While Loop in C

Do While Loop in C Program with Examples

In this article, I am going to discuss the Do While loop in C Program with Examples. Please read our previous articles, where we discussed While loop in C 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:

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

Syntax to use Do While Loop in C:

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:

Program to understand do while loop in c

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:

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

Program:

#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:

C Nested Do while loop

In the next article, I am going to discuss For Loop in C Program with examples. Here, in this article, I try to explain Do While Loop in C Program with examples. I hope you enjoy this 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. Required fields are marked *