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 {#practicing-while-loops-warm-up}
Create a new project, and replace the content of the Main
method with
the following code:
- 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:
or
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 {#practicing-while-loops-decrementing-counter}
Create a new project and replace the content of the Main
method with
the following code:
Execute the code, and explain what you see in the console. Note that the counter is decremented, not incremented.
Practicing while Loops — Mystery Program {#practicing-while-loops-mystery-program}
Create a new project and replace the content of the Main
method with
the following code:
- 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 {#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 execution, where the user input is u͟n͟d͟e͟r͟l͟i͟n͟e͟d͟, and hitting “enter” is represented by “⏎͟”:
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://princomp.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.
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:
- 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.