The Do While Loop is different from the While Loop for one reason only. The condition is checked after executing the statements. Therefore, this loop guarantees that the statements will be executed at least once.

Do While Loop
The syntax for the Do While Loop is below:
do { // Statements ... } while ( condition ); |
Do While Example
An example of a do while loop is below.
1 2 3 4 5 6 7 | string msg; do { cout << "Enter a word or 'exit' to quit: " << endl; cin >> msg; } while ( msg != "exit" ); |
Do While Loops aren’t used as extensively as While Loops and For Loops in practice due to their nature. They are only used for specific cases where the programmer needs the block of statements to be executed at least once.
Going further with Do While Loops
Now that you understand Do While Loops, While Loops, and For Loops why not try the following?
- Create a factorial program that uses a while, do while, and for loop. Do the same for summation. This should make you fully understand all loops and when to use them.
