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
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
The syntax of the do-while loop
do{ //statement(s) to be executed }while(condition);
flow diagram of while loop in Java
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++
#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++
#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++
#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++
Suggested for you
Nested while in C++
Nested while in Python
Nested do while in Java
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…