C++ do-while loop is used to execute one or more statements while the condition remains true. The major difference between a while loop and a do-while loop is:
While the loop executes its statements after the condition if the condition becomes false at the beginning the loop is terminated.
On the other hand, the do-while loop executes its statements before the condition if the condition becomes false at the beginning the do-while loop runs at least once.
Syntax: do-while loop in C++
do{
code block;
}
while(condition)
Flow Chart: do while loop
Example: do while loop in c++
//do while loop in C++
#include <iostream>
using namespace std;
int main()
{
int i = 1;
do {
cout << i <<" Hello World!"<<endl;
++i;
}
while (i <= 10);
return 0;
}
Output
1 Hello World!
2 Hello World!
3 Hello World!
4 Hello World!
5 Hello World!
6 Hello World!
7 Hello World!
8 Hello World!
9 Hello World!
10 Hello World!