C++ Do While loop


Go to main Page

Do While Loop

The syntax of Do While Loop is:

do {
   // body of loop;
}
while (condition);

The code execute first.Than Do While loop evaluates the condition.

If conditions become true the code will evaluate again.

The condition evaluates again.

Until the condition become false.

Diagram
Example 1: Print number 1 to 5

#include <iostream>
using namespace std;
int main() {
    int i = 1; 
    do {
        cout << i << " ";
        ++i;
    }
    while (i <= 5);
    return 0;
}

Output

1 2 3 4 5

Explaination