The Do-While loop is similar to the while loop. It is a conditional loop, just like the while loop, running the body of the loop while the condition is met.
However, the big difference is that the do-while loop is a post-test loop. That means that the body runs at least once, and then there is a while statement to determine if it will run again. This is unlike a while loop who’s body may never run.
Consider the general form:
do
{
// the loop body
} while( <condition>);
This often lends itself to work a little easier with getting user input, as you don’t have to ask for it twice, you just use the do-while loop, and only use a single cout / cin combo.
Consider the following example, where we mimic the sentinel loop:
int sum = 0;
int number;
do
{
cout << "Enter a number to be added to be summed: ";
cin >> number;
sum += number;
} while(number != 0);
cout << "The sum of the numbers is: " << sum << endl;
Notice that this makes the code a little smaller/tighter, and logically may make more sense for some people.
The C++ Do-While Loop was originally found on Access 2 Learn