In this tutorial, we will discuss do while loop in C programming language
In the C language, there are three types of basic looping statements
for loop
do while loop
In the C programming language, do- while loop is used for execution and evaluation of C code repeatedly until the test expression is false. When the do-while loop is executed. The test expression is evaluated until the condition is satisfied. Do while loop is executed at least once before the while part is executed.
Do while loop contains two-part
The syntax of do while loop
do{ //statement(s) to be executed }while(condition);
flow diagram of while loop in Java
According to the above diagram, initially, execution starts and flow of control enters the body of the do-while loop and statement are executed only once.
Then, the test expression is evaluated.
When the test expression is true, this process continues until the test expression becomes false.
When test expression is false, the do-while loop stops the execution and goes to rest.
Program 1
#include <stdio.h> #include <stdlib.h> int main() { printf("Do while loop in C\n"); int i=1; do{ printf("value of i is : %d\n",i); i++; }while(i<=10); getch(); return 0; }
When the above code is executed, it produces the following results:
Do while loop in C value of i is : 1 value of i is : 2 value of i is : 3 value of i is : 4 value of i is : 5 value of i is : 6 value of i is : 7 value of i is : 8 value of i is : 9 value of i is : 10
In above the program of C language, it displays 1 to 10 natural numbers
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int sum=0,i=1; //initialization variables sum and i do{ sum=sum+i; i++; //increment of counter variable in every cycle }while(i<=10); printf("sum of first 10 nutural numbers are : %d",sum); getch(); return 0; }
When the above code is executed, it produces the following results:
Sum of the first ten natural numbers is: 55
In this program of C language, it calculates the sum of the first ten natural numbers
Program 3
#include <stdio.h> #include <conio.h> int main() { int n,i=1,f=1; printf("\nEnter the number:"); scanf("%d",&n); do{ f=f*i; i++; }while(i<=n); printf("\n The factorial of %d is %d",n,f); }
When the above code is executed, it produces the following results:
Enter the number :6 The factorial of 6 is 720
In this program of C language, it calculates the factorial of given numbers
#include <stdio.h> #include <stdlib.h> int main() { printf("Array elements printed here\n"); int marks []={56,35,76,43,91,58}; //array index with start with Zero //So "i" started always with zero int i=0; do{ printf("%d\n",marks[i]); i++; }while(i<=5); getch(); return 0; }
When the above code is executed, it produces the following results:
Array elements printed here 56 35 76 43 91 58
In this program of C language, it displays a single dimension array of elements
Program 1
#include <stdio.h> #include <stdlib.h> int main() { do{ printf("this is an infinitive do while loop in C\n"); }while(1==1); getch(); return 0; }
When the above code is executed, it produces the following results:
this is an infinitive do while loop in C
this is an infinitive do while loop in C
this is an infinitive do while loop in C
this is an infinitive do while loop in C
this is an infinitive do while loop in C
this is an infinitive do while loop in C
this is an infinitive do while loop in C
…………………….
……………………..
this loop is printed infinite time
Suggested for you
Nested for loop in C++ language
Nested for loop in Java language
Nested for loop in Python language
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…