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 syntax

The syntax of the C language do-while loop is given below:



  1. do{  
  2. //code to be executed  
  3. }while(condition); 


Example 1



  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. void main ()  
  4. {  
  5.     char c;  
  6.     int choice,dummy;    
  7.     do{  
  8.     printf("\n1. Print Hello\n2. Print Javatpoint\n3. Exit\n");  
  9.     scanf("%d",&choice);  
  10.     switch(choice)  
  11.     {  
  12.         case 1 :   
  13.         printf("Hello");   
  14.         break;  
  15.         case 2:    
  16.         printf("Javatpoint");  
  17.         break;  
  18.         case 3:  
  19.         exit(0);   
  20.         break;  
  21.         default:   
  22.         printf("please enter valid choice");      
  23.     }  
  24.     printf("do you want to enter more?");   
  25.     scanf("%d",&dummy);  
  26.     scanf("%c",&c);  
  27.     }while(c=='y');  
  28. }  

Output

1. Print Hello
2. Print Javatpoint
3. Exit
1
Hello
do you want to enter more?
y

1. Print Hello
2. Print Javatpoint
3. Exit
2
Javatpoint
do you want to enter more?
n



Jay Kakadiya

Jay Kakadiya Creator

I am a computer field, & i am Web developer.

Suggested Creators

Jay Kakadiya