This lab serves multiple goals:
- To reinforce your understanding of
for
loops, - To train you to convert between loop formats,
- To practice solving simple problems using
for
loops, - (Optional) To introduce the keywords
break
andcontinue
, - (Optional) To teach you about the “true form” of
for
loops.
From while
to for
Rewrite the following while
(or do...while
) loops as for
loops.
This should “just” be a matter of re-ordering the code, and you should
be able to do it without thinking much about it.
From for
to while
Rewrite the following for
loops as while
loops:
Implementing for Loops
This exercise is to practice for
loops.
Write a program that asks the user to enter a positive integer, and then
uses a for
loop to compute the sum of all the integers between and
the integer given by the user. For instance, if the user enters ,
your program should display on the screen (i.e.,
). You are asked to implement user-input
validation later on in this exercise, so you can assume for now that
users will always provide numbers.
Then, answer the following questions:
-
Without executing your program, can you tell what will happen if the user enters a negative value?
-
Do you think you could have written the same program using a
while
loop? -
How would you change the program to make it compute the product instead of the sum (i.e., for , )?
-
How would you change the program to make it display on the screen the divisors of the integer entered? Examples:
- divisors of are: ,
- divisors of are: , , , ?
You can modify your program to check your answers to the previous questions. Once you are done, modify your original program in these two respects:
- Once the result of the computation is displayed on the screen, ask the user if they want to compute the sum using another integer or quit and act accordingly.
- Add some input validation: floating-point values, non-numeric strings, and negative values should not be allowed (i.e., your program should ask for another value).
Pushing Further (Optional)
Multiple Initializations and Updates
This section is about two modifications of for
loops that are
sometimes considered bad design; used poorly, they can make the code
harder to read and debug, and sometimes make it hard to follow the flow
of control of your program. They are introduced because you may see them
in the future, but except for rare cases, should be avoided in your own
code. The exact structure of for
loops is actually more complex than
discussed in class. It is
That is, there can be more than one initialization assuming the variables all have the same datatype and more than one update. This means there are legal statements like:
or
Also, the initialization and update condition are actually optional; we could have
and
Try to rewrite the four for
loops just given as “ordinary” for
loops
with exactly one initialization and one update in the header of the
for
loop.
Using continue
and break
Programmers can use two keywords in loops that modify the control flow;
they are continue
and break
. They can make the loop more confusing
to read, but they can sometimes be useful for reducing the number of
nested if
statements in a complex loop. Try executing the following
code to see what these statements do.
You can also use break
and continue
in while
loops. Try to rewrite
the previous two for
loops as while
loops. There is a trick to make
the while
loop using continue
work properly; can you spot it?