Introducing Loops and Complex Conditions
This set of Mini-Lab Exercises is the third in a series in which students build a small program with several fish moving around in an aquarium. The set includes the following exercises:
Each section contains an Introduction to a problem or task, descriptions or examples of one or more Concepts to apply in solving the problem or completing the task, and an Exercise.
In the exercises that precede this one, students will have created three fish,
moved them forward one step (changing the fish's direction when it is about
to hit a wall), and displayed them graphically. Students should be familiar
with constructing objects, invoking methods, using conditional statements and
logical expressions (including the
operators), and the
When writing a program, we sometimes want to repeat a given action or set of actions a known number of times. The number of times to repeat the action, which we call N, might be a constant number or a value in a variable.
for statement (also known as a
loop) provides an easy way to do this.
The structure of a
for statement is shown below.
To repeat a set of actions, enclose the set in curly braces, just as you
would do for multiple actions in a conditional statement.
for ( <initialization> ; <condition> ; <step> )
There are several ways to use a
for statement, each of
which is useful for different purposes. For a counted repetition, we
create a variable to count the number of times the action has been
repeated so far.
variable is created and initialized and then, so long as the action has
not been repeated too many times (the "condition"), the action is done and
the count is incremented (the "step"). There are two common ways to do
this: counting from 1 to N, or counting from 0 to N-1. The second
approach may not seem as intuitively obvious as the first, but it is
very useful in other contexts (such as stepping through all of
the items in a list).
consider a code fragment that deals a single card to each player in a
card game. Both examples below repeat the
nbrPlayer times, so
nbrPlayer is the N
in this example.
|Counting from 1 to N
(Repeats N times)
|Counting from 0 to N-1
(Repeats N times)
Notice that both styles express the condition in terms of N
nbrPlayers), but one uses the
less than or equal to (
operator, while the other uses the
less than (
WARNING: It is very important not to mix these two approaches, or you will execute the actions in your loop the wrong number of times!
|Error Counting from 1
(Repeats N-1 times)
|Error Counting from 0
(Repeats N+1 times)
These are known as off-by-one boundary errors, and are very easy to make. To avoid these kinds of mistakes, many people choose to memorize and consistently use the approach counting from 0 to N-1, since that is the approach needed to step through lists of items.
for ( int i = 0; i < N; i++ )
if statements, it is best to
for loop conditions as a
Positive Condition if possible.
When we want to get some input interactively from the user, we need to prompt the user for the input, read the input, and verify that the input meets the expectations of the program. For example, if a program needs a student's name and student ID, then the prompts should make it clear when the program is asking for a name and when it is asking for a student ID. If the student id is always six numeric digits, then the program should check that the ID provided by the user is six numeric digits, and not a alphabetic string, floating point number, or other type. If the input does not meet the expectations of the program, the user should be provided with another opportunity to enter the data, possibly with a new prompt that provides clearer instructions about what the expectations are.
We can write a complex boolean expression using the AND (&&) or OR (||) operators to check for two conditions, whether the fish is at a wall and so must change direction, or has randomly chosen to change direction even though it is not at a wall.