do while loop in C

Course- C >

To execute a part of program or code several times, we can use do-while loop of C language. The code given between the do and while block will be executed until condition is true.

In do while loop, statement is given before the condition, so statement or code will be executed at lease one time. In other words, we can say it is executed 1 or more times.

It is better if you have to execute the code at least once.

do while loop syntax

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

 

do{  

//code to be executed  

}while(condition);  

Flowchart of do while loop

DO WHILE IN C

do while example

There is given the simple program of c language do while loop where we are printing the table of 1.

 
  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1;  
  5. clrscr();    
  6.   
  7. do{  
  8. printf("%d \n",i);  
  9. i++;  
  10. }while(i<=10);  
  11.     
  12. getch();    
  13. }    

Output

1
2
3
4
5
6
7
8
9
10

Program to print table for the given number using do while loop

 
  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1,number=0;  
  5. clrscr();    
  6.   
  7. printf("Enter a number: ");  
  8. scanf("%d",&number);  
  9.   
  10. do{  
  11. printf("%d \n",(number*i));  
  12. i++;  
  13. }while(i<=10);  
  14.     
  15. getch();    
  16. }    

Output

Enter a number: 5
5
10
15
20
25
30
35
40
45
50
Enter a number: 10
10
20
30
40
50
60
70
80
90
100

Infinitive do while loop

If you pass 1 as a expression in do while loop, it will run infinite number of times.

 
  1. do{  
  2. //statement  
  3. }while(1);