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

Using the do-while loop, we can repeat the execution of several parts of the statements. The do-while loop is mainly used when 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:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}while (j<=3);
return 0;
}
#include <stdio.h> int main() { int j=0; do { printf("Value of variable j is: %d\n", j); j++; }while (j<=3); return 0; }
#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: You need to use the do-while loop when you want to execute the loop at least once, irrespective of the condition.

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 for using the nested do-while loop in C language is below.

Nested do-while Loop in C Language

Program to Understand Nested do-while Loop in C Language:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#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;
}
#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; }
#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. 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, questions, or comments about this article.

Leave a Reply

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