This lab serves multiple goals:
- To reinforce your understanding of the syntax of
do…while
loops, - To stress the differences between
while
anddo…while
loops, and - (Optional) To use a
do…while
loop to solve a simple problem.
Implementing do…while
Loops — Warm-up {#implementing-dowhile-loops-warm-up}
For all the problems in this section, use a do…while
loop.
-
Write a program that displays the numbers 0 to 50.
-
Write a program that displays the numbers 30 to -20.
-
Write a
do…while
loop that generates this output:
User Input Validation using do…while
Loops
In the following problem, implement a program combining a do…while
loop with user input that satisfies the following requirements:
- Ask the user to enter an integer between (and including) 0 and 100.
- If the value provided by the user is not in this range, the program should repeat the question.
- After the user provides an integer within the range, display that number.
Here is an example of execution, where the user input is u͟n͟d͟e͟r͟l͟i͟n͟e͟d͟, and hitting “enter” is represented by “⏎͟”:
Comparing while
and do…while
loops
Consider the program given below that has been implemented using a
while
loop:
- Convert the program to an equivalent version that uses a
do…while
loop. - Test the
do…while
version with different inputs to ensure the behavior is the same and that the program does not crash with an error. For example, you should try:- alphabetic input (e.g., “Train”) which would be invalid,
- floating point numbers (e.g., “12.5”) which would be invalid,
- negative integers (e.g., “-12”) which would be valid,
- positive integers (e.g., “10”) which would be valid, and
- the number 0 which would be valid.
- Compare the
while
anddo…while
implementations: which one is better, in your opinion, and why?
Pushing Further (Optional)
This time, you are given a simple task without much guidance. This is very similar to the type of questions you may face during exams:
Write a program that asks the user to enter a temperature in degrees Fahrenheit. If the user enters something that is not a number or enters a number outside the “sensible” range of -20 to 120 degrees (both included), your program should ask for the temperature again, and it should keep asking until the user provides valid input.
Once the user has entered a “sensible” temperature value, your program should initialize a string variable named description to one of the following values: “Cold” if the temperature is below 40 degrees, “Mild” if the temperature is between 40 and 70 degrees, or “Warm” if the temperature is above 70 degrees. This string should then be displayed.
A possible solution is given below.
Solution:
A possible solution, using do…while
is: