A do while
loop is the same as a while
loop, except that it is guaranteed to execute at least one time.
The following loop will execute 100 times.
int i = 0;
do {
i++;
} while (i < 100);
A similar loop, but with a different condition, will execute 1 time.
int i = 0;
do {
i++;
} while (i < 0);
If the above loop were merely a while
loop, it would execute 0 times, because the condition would evaluate to false
before the first iteration. But since it is a do while
loop, it executes once, then checks its condition before executing again.