Iteration with the do while loop
The do while
loop has the same features as the while
loop. do while
works on a condition and can be used when we don't know how many iterations we will need to make.
The difference from the while
loop is that where a while
loop might never execute because the condition could be false
the first time we test it. In comparison, the do while
loop is guaranteed to run at least once. The reason for this is because the condition is moved from the beginning of the loop to the end of it.
This can be good for several reasons, and it can make our guessing game slightly less complicated. However, before we do that, we should look at what a do while
loop looks like:
do … some code that eventually sets the condition to false while condition
The do
keyword marks the beginning of the loop. As you can see, there is nothing more on this line, so the program must run through the code inside the loop...