Do While

As we've seen in While loops, the loop will only start the loop if the initial condition is met.

A do-while loop however guarantees us that the loop will run at least once, because the condition is tested after the loop has iterated. This can be useful if we want the loop to execute at least once, and optionally continue after that.

The syntax for a do-while loop is as follows:

do {
    //do something
} while (conditionIsMet);
Note the sneaky semicolon at the end of the while clause

It is imperative to know that variables used in the the while clause (conditionIsMet in the above example) must be declared before the loop (though they can be modified within the loop)


In the loops tasks, one of them required you to prompt the user if they wanted another verse of the ten green bottles song. This time, re-write that code as a do-while loop, so the first verse is always printed, but then the user is prompted if they want another verse. At this stage, do not worry about exiting the loop when you run out of bottles, the user can choose to continue with a negative number of bottles if they wish. Hint: you will need a variable declared outside the loop to store their response in.