CPP Do While Loop is a loop statement which is used to execute a block of code continuously as long as the condition of the loop is true, and stops only when the condition fails like other loop statements. The unique feature of Do While loop is that it is used to execute a block of code at least once and then to repeat the actions as long as the specified condition is true.
Syntax:
do
{
code to be executed;
}
while (condition);
Example 1: When the While condition is true
#include <iostream.h> using namespace std; int main() { int i = 1; do { cout << i; i++; } while (i <= 10); return 0; } |
Output
12345678910 |
Example 2: When the While condition is false
#include <iostream.h> using namespace std; int main() { int i = 11; do { cout << i; i++; } while (i <= 10); return 0; } |
Output
11 |