Table of Contents
C Programming Language Do-while loop
In this tutorial, we will discuss the concept of C programming Language Do-while loop
In this post, we are going to learn how to use the do-while loop in C language
Do-while loop
What is the usage of the do-while loop in C? you can understanding after reading this article .
generally in programming languages, looping statements used in programming language to executes of block of code until the perticular condition is satisfied.
C program has 3 looping statement
- for loop
- while loop
- do-while loop
Do-while loop
Syntex
Flow diagram – do-while loop
The do-while loop is similar to while loop but one main difference between while and do-while loop. The do-while loop is executed only once time before checking the condition part. So you will understand the do-while loop is executed at least once.
How to work do-while loop
First,the do-while loop executes only once. then the text expression is evaluated, if the test expression becomes true, the codes inside the loop body is executed again. The process goes on until the test expression is false when the test expression is false, the do-while loop is terminated
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1;
do{
printf(“%i”,i);
i++;
}
while(i<=10);
//printf(“Hello world!n”);
return 0;
}
In the program, test expression returns true codes inside the loop body are executes until become condition is false, so output here
Te program displays natural numbars 1 to given number
Program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=1;
do{
printf(“%in”,i);
i++;
}
while(i>=10);
return 0;
}
When the above code is executed, it produces the following result
#include <iostream> #include <conio.h> using namespace std; int main() { int sum=0; int n; cout << "Enter the number as you wish" << endl; cin>>n; int i=1; do{ sum=sum+i; i++; }while(i<=n); cout<<"The sum of 1 to "<<n<<" : "<<sum; getch(); return 0; }
When the above code is executed, it produces the following result
Enter the number as you wish 100 The sum of 1 to 100: 5050