What you're missing is that your use of do...while
above is 100% WRONG!
A do...while
loop in Java, for example, looks something like this:
do {
//something you want to execute at least once
} while (someBooleanCondition);
See that complete lack of a second block there? See how the while
ends the statement completely? So what happens now is that the code in the {...}
pair between do
and while
will get executed, then someBooleanCondition
will get tested and if it's true, the code block will get executed again. And again. And again. Until someBooleanCondition
gets tested false.
And at this point, perhaps, you'll understand why we have both forms of loop. The above code could be translated to:
//something you want to execute at least once
while (someBooleanCondition) {
//something you want to execute at least once
}
This code, however, requires you to type the same (potentially large and complicated) code twice, leaving the door open for more errors. Hence the need for a bottom-test loop instead of a top-test loop.