do-while Loop

This lesson explains how do-while loop is implemented and how it is different from the while loop.

In a previous lesson, we saw the steps in which the while loop is executed:

preparation

condition check
actual work
iteration

condition check
actual work
iteration
...

The do-while loop is very similar to the while loop. The difference is that the condition check is performed at the end of each iteration of the do-while loop so that the actual work is performed at least once:

preparation

actual work
iteration
condition check  ← at the end of the iteration

actual work
iteration
condition check  ← at the end of the iteration
...

For example, do-while may be more natural in the following program, where the user guesses a number. The user must guess at least once so that the number can be compared:

Get hands-on with 1200+ tech skills courses.