This lab serves multiple goals:
do…while
loops,while
and do…while
loops, anddo…while
loop to solve a simple problem.do…while
Loops – Warm-upFor 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:
1 10 100 1000 10000 100000 1000000
do…while
LoopsIn the following problem, implement a program combining a do…while
loop with user input that satisfies the following requirements:
Here is an example of a possible interaction with the program, where the user input is underlined, and hitting “enter” is represented by ↵:
Enter an integer between 0 and 100:
N͟O͟↵
Sorry, that is not a valid integer.
Enter an integer between 0 and 100:
-͟2͟0͟↵
Sorry, that is not a valid integer.
Enter an integer between 0 and 100:
4͟2͟↵
You entered 42.
while
and do…while
loopsConsider the program given below that has been implemented using a
while
loop:
int n;
bool flag = false;
Console.Write("Enter an integer:");
flag = int.TryParse(Console.ReadLine(), out n);
while(!flag)
{
Console.WriteLine("The value you entered is not a valid integer.");
Console.Write("Enter an integer:");
flag = int.TryParse(Console.ReadLine(), out n);
}
Console.WriteLine($"The number you entered is {n}");
do…while
loop.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:
while
and do…while
implementations: which one is
better, in your opinion, and why?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.