Do While Loop in C Language with Examples

Do While Loop in C Language with Examples



do while:
'do while' is a keyword in java language which is used to execute one block of statements several number of times based on the condition.
Syntax:
do
{
    statement 1
    statement 2
    ..
    ..
}while(condition);


In the above syntax if the condition is true the statements of do while block is execute infinite times, if the condition is false the statements of do while block is executed one time

Display 1 to 10 values using do while loop

#include <stdio.h>
int main() {
    int x=1;
    do
    {
        printf("%d\n",x);
        x++;
    }while(x<=10);
    return 0;
}
                                            

Output:

1
2
3
4
5
6
7
8
9
10


More Programs

Write a C-program to print 1 to n values using while loop

Write a C-program to print even numbers using while loop

Write a C-program to print even number or odd number based on the n value using while loop

Write a C-program to print even number or odd number based on the n value using single while loop

Write a C-program to count the number of divisible numbers in a given range(1-100) using while loop

Write a C-program to print factorial number using while loop

Nested do while loop
One do while loop within another do while loop is knows as Nested do while Loop Syntax:
do
{
    do
    {
         statement 1
         statement 2
    }while(condition);
    statement 3
    statement 4
      
}while(condition);



#include <stdio.h>
                                                                         
int main() {
    int x=1,y;
    do
    {
         y=1;
         do
         {
             printf("*");
             y++;
         } while(y<=3);
         printf("\n");
         x++;
    }while(x<=6);
    return 0;
}
                                            

Output:

***
***
***
***
***
***


More Programs

Write a C-program to print ASCII values of A to Z

Write a C-program to print ASCII values of Numbers