while Loop
May 22, 2024 (01:31:00 PM)
This lab serves multiple goals:
- To reinforce your understanding of the syntax of
while
loops, - To stress the importance of conditions (e.g., the difference between
<
and<=
), - To study loops whose counter starts at values different from
0
, - To exhibit that loop counters can be incremented or decremented by any value (not just one),
- To detect and debug infinite loops,
- To design simple algorithms requiring loops, and
- (Optional) To have loops controlled by sentinel values.
Practicing while Loops – Warm-Up
Create a new project, and replace the content of the Main
method with the following code:
int i = 0;
while(i < 100)
{
.WriteLine(i);
Console++;
i}
- Execute the code. You should see the numbers 0 to 99 in the console.
- Replace
<
with<=
, and note that it prints the numbers from 0 to 100, even even though you did not change the numbers. - Replace
0
with100
and100
with300
, and note that it prints the numbers from 100 to 300. Observe that the counter can start from and terminate with any number you wish. - Modify the code such that it prints all integers between 0 and 100 that are divisible by 3. The solution is given below, but please attempt to do it independently before reading it.
Solution
To implement the above problem, you may use the following:
int i = 0;
while(i < 100)
{
if(i % 3 == 0)
.WriteLine(i);
Console++;
i}
or
int i = 0;
while(i < 100)
{
.WriteLine(i);
Console+= 3;
i }
Which one of the above codes seems more efficient / easier to understand / easier to debug?
Note that you do not have to increment the counter only by one each time. You should update the counter wisely and try to use it more efficiently.Practicing while Loops – Decrementing Counter
Create a new project and replace the content of the Main
method with the following code:
int n = 100;
while (n > 0)
{
.WriteLine(n);
Console--;
n}
Execute the code, and explain what you see in the console. Note that the counter is decremented, not incremented.
Practicing while Loops – Mystery Program
Create a new project and replace the content of the Main
method with the following code:
int n;
.Write("Enter a natural number greater than 2: ");
Console= int.Parse(Console.ReadLine());
n int i = 2;
while(n % i != 0 && i < n )
{
++;
i}
if (i == n)
.WriteLine($"{n} is a ... number");
Consoleelse
.WriteLine($"{n} is not a ... number"); Console
- What does the code do? Explain the boolean expression of the loop
- Replace
...
with a meaningful word.
Solution
The boolean expression uses a counter
i
, whose original value is 2, and then checks if:- the result of the division of
n
byi
is 0 (stated differently: whetheri
can dividen
), i
is less thann
. In other words, it tries to dividen
by all the numbers between 2 andn-1
, and exits if there is a number that dividesn
.
- the result of the division of
This program computes if the number entered by the user is prime! So, we should replace
...
with “prime”!
Practicing while Loops – Summing User-Input
Write a program that asks for an integer value greater than 1 from
the user, and computes the result of this series: 1 + 2 + 3 + 4 + ...
up to n
where n
represents the number obtained from the
user.
Here is an example of the desired execution, where the user input is underlined, and hitting “enter” is represented by ↵:
Please enter an integer greater than 1:
8̲↵
The sum from 1 to your number is: 36
And you can verify for yourself that 1+2+3+4+5+6+7+8 = 36.
Solution
You can look at the code under “Accumulator” at https://csci-1301.github.io/book.html#vocabulary-1 to get started: essentially, you need to replace the fixed value 10 with the value given by the user.Infinite Loops
All of the following are examples of infinite loops. Can you spot the “problem”? For each of them, suggest an edit that would make them terminate.
int number = 0;
while (number <=5) {
.WriteLine("Hi!");
Console.WriteLine(number);
Console}
int number1 = 0, number = 0;
while (number <=5) {
.WriteLine("Hi!");
Console.WriteLine(number);
Console++;
number1}
int number = 0;
while (number <=5);
{
.WriteLine("Hi!");
Console.WriteLine(number);
Console++;
number}
int number = 0;
while (number <=5)
.WriteLine("Hi!");
Console.WriteLine(number);
Console++; number
int number = 0;
while (number <= 5)
{
.WriteLine("Hi!");
Console.WriteLine(number);
Console}
++; number
int number = 0;
while (number <= 5)
{
.WriteLine("Hi!");
Console--;
number.WriteLine(number);
Console++;
number}
Pushing Further (Optional)
Here are two advanced while
loop
challenges. Try to think “off-keyboard” for a while before coding your
solution, and test it extensively.
Advanced Problem 1
Study the following program:
.WriteLine("Enter a number to sum, or \"Done\" to stop and print the total.");
Consolestring enter = Console.ReadLine();
int sum = 0;
while (enter != "Done")
{
+= int.Parse(enter);
sum .WriteLine("Enter a number to sum, or \"Done\" to stop and print the total.");
Console= Console.ReadLine();
enter }
.WriteLine($"Your total is {sum}."); Console
- Execute it, and make sure you understand its mechanism.
- It contains a “sentinel value”: can you tell what it is?
- Write a program by taking inspiration from the previous program. Your program should ask the user to enter integers. After the user indicates they are done (by entering a sentinel value like “Done”), display the smallest value the user entered. If the user did not enter any integers, display “You did not enter anything.”
Advanced Problem 2
Write a program that gets a number from the user and finds its biggest divisor less than the number itself.