Do while loop in Cpp programming language
- Home
- Loop
- While loop
- Do while loop in Cpp programming language
- On
- By
- 0 Comment
- Categories: While loop
Do while loop in Cpp programming language
Do while loop in Cpp programming language
In this tutorial, we will discuss do while loop in Cpp programming language
In the C++ language, there are three types of basic looping statements
do while loop
do while loop
In the C++language, do- while loop is used for execution and evaluation of body of code repeatedly until the test expression is false. The do-while loop is executed when the Boolean expression is evaluated until the condition is satisfied. Do while loop is executed at least once before while part is executed.
Do while loop contains two-part
- Do part – Do part is a staring point of do while loo. do part is always executed.
- While part– Do part is a processing point of the do-while loop.
Declaration
The syntax of the do-while loop
do{ //statement(s) to be executed }while(condition);
flow diagram of while loop in Java
How do-while loop works:
In the above diagram, initially, execution starts and flow of control enters the body of the do-while loop and statement is 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 <iostream> #include <conio.h> using namespace std; int main() { int i=1; do{ cout<<"i is :"<<i<<endl; i++; }while(i<=10); //when the test expression is true getch(); return 0; }
When the above code is executed, it produces the following results:
i is :1 i is :2 i is :3 i is :4 i is :5 i is :6 i is :7 i is :8 i is :9 i is :10
In the above program, it displays 1 to 1 positive number using the do-while loop in C++
Find the sum of given numbers using the do-while loop
#include <iostream> #include <conio.h> using namespace std; int main() { int sum=0,i=0; int n; i++; cout<<"Enter number for n :"; cin>>n; do{ sum+=i; i++; }while(i<=n); cout << "Total is: " << sum<<endl; getch(); return 0; }
When the above code is executed, it produces the following results:
Enter number for n :10 Total is :55
In the above program, it finds the sum of given positive numbers using the do-while loop in C++
Displaying Array element in the used Do-while loop
#include <iostream> #include <conio.h> using namespace std; int main() { string name[]={"Jhon","Khan","Robert","Seela"}; //array index starts with zero int i=0; do{ cout<<"Student name "<<i+1<<" is "<<name[i]<<endl; i++; }while(i<=3); getch(); return 0; }
When the above code is executed, it produces the following results:
Student name 1 is :Jhon Student name 2 is :Khan Student name 1 is :Robert Student name 1 is :Seela
In the above program, it displays single dimension array of elements using the do-while loop in C++
Find factorial of a number in C++ language
#include <iostream> #include <conio.h> using namespace std; int main() { int n,i=1,f=1; cout << "Enter the number" << endl; cin>>n; //Do while loop to calculate factorial do { f=f*i; i++; }while(i<=n); cout << "The factorial of " <<n<<" is "<<f<< endl; getch(); return 0; }
When the above code is executed, it produces the following results:
Enter the number 5 The factorial of 5 is:120
In the above program, it finds the factorial of given positive numbers using the do-while loop in C++
Similar post
Suggested for you
Nested for loop in C++ language
Nested for loop in Java language
Nested for loop in Python language