Skip to main content
So far you have only worked with one type of loop: the while loop. But there is another type: the “do-while” loop.The do-while loop works almost exactly like a while loop. In fact, most of the time they are equivalent. Examine the program below to see if you can figure out the tiny difference.Files NeededSample OutputGoofus and Gallant are both going swimming. They hate to swim in cold water; once the water temperature drops below 79°F, they stop.Run the program, and type in 80.5 for the water temperature.
What is the current water temperature? 80.5

Okay, so the current water temperature is 80.5F.
GALLANT approaches the lake....
        GALLANT swims for a bit. Swim time: 1 min.
        The current water temperature is now 80.0F.
        GALLANT swims for a bit. Swim time: 2 min.
        The current water temperature is now 79.5F.
        GALLANT swims for a bit. Swim time: 3 min.
        The current water temperature is now 79.0F.
        GALLANT swims for a bit. Swim time: 4 min.
        The current water temperature is now 78.5F.
GALLANT stops swimming. Total swim time: 4 min.

Okay, so the current water temperature is 80.5F.
GOOFUS  approaches the lake....
        GOOFUS  swims for a bit. Swim time: 1 min.
        The current water temperature is now 80.0F.
        GOOFUS  swims for a bit. Swim time: 2 min.
        The current water temperature is now 79.5F.
        GOOFUS  swims for a bit. Swim time: 3 min.
        The current water temperature is now 79.0F.
        GOOFUS  swims for a bit. Swim time: 4 min.
        The current water temperature is now 78.5F.
GOOFUS  stops swimming. Total swim time: 4 min.
In this program, you’ll see how using a do-while loop might be better than a while loop.Files NeededSample OutputThe code I have provided does not compile. Once you fix it, it will look roughly like this.
You flip a coin and it is... TAILS
Would you like to flip again (y/n)? y
You flip a coin and it is... HEADS
Would you like to flip again (y/n)? y
You flip a coin and it is... HEADS
Would you like to flip again (y/n)? n
Redo the Dice Doubles assignment (the dice program with a loop) so that it uses a do-while loop instead of a while loop. Otherwise it should behave exactly the same.If you do this correctly, there should be less code in this version.Sample Output
HERE COME THE DICE!

Roll #1: 3
Roll #2: 5
The total is 8!

Roll #1: 6
Roll #2: 1
The total is 7!

Roll #1: 2
Roll #2: 5
The total is 7!

Roll #1: 1
Roll #2: 1
The total is 2!
Redo the Number-Guessing with a Counter assignment using a do-while loop instead of a while loop. Otherwise it should do exactly the same things (including the counter).Make sure that it doesn’t mess up if you guess it on the first try.Sample Output
I have chosen a number between 1 and 10.  Try to guess it.
Your guess: 5
That is incorrect.  Guess again.
Your guess: 4
That is incorrect.  Guess again.
Your guess: 8
That is incorrect.  Guess again.
Your guess: 6
That's right!  You're a good guesser.
It only took you 4 tries.
Write a calculator program that allows the user to add, subtract, multiply or divide two numbers. (This is sometimes called a “four-function” calculator.)The program must loop until they enter a zero as the first number.Files NeededSample Output
>2 + 3
5
>4 * 9
36
>0 + 2
Bye, now.