The do/while loop
The
do/while
loop is almost identical to the while
loop. Here's an example of a do/while
loop that is equivalent to the first while
loop that we examined:
int x = 1; do { cout << "x is " << x << endl; x++; } while( x <= 5 ); // may only loop back when x<=5 cout << "End of program" << endl;
The only difference here is that we don't have to check the while
condition on our first entry into the loop. This means that the do/while
loop's body is always executed at least once (where a while
loop can be skipped entirely if the condition to enter the while
loop is false
when you hit it for the first time).