Aquarium Lab Series
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, (usually) abridged versions of one or more Patterns that will be useful 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 && and ||
operators), and the
RandNumGenerator class.
Students should read over the patterns that appear in this document before the lab.
You are in a situation in which some action should be repeated a known number (N) of times, where N might be a constant number or a value in a variable. The number of repetitions does not depend on any aspect of the repeated action, i.e., you know the value of N before you start the repetition.
Therefore, use a FOR statement that creates a variable to count the number of times the action has been repeated. The counting variable is created and initialized and then, so long as the action has not been repeated too many times (the "test"), the action is done and the count is incremented (the "step"). Express the test as a Positive Condition if possible.
The structure of a FOR loop is:
for (<initialization> ; <test> ; <step>)<repeated action>
There are two common idioms for controlling the loop. The first is to count from 0 to N-1, which is the idiom used to ensure a Valid Index in a Linear Indexed Traversal (see the Repetition pattern for detailed information on Linear Indexed Traversal). For example, at the beginning of a card game each player is dealt a hand of cards:
for (int i = 0; i < nbrPlayers; i++)
{ dealHand();
}
The second common idiom for controlling the loop is to count from 1 to N. For example,
for (int i = 1; i <= nbrPlayers; i++)
{ dealHand();
}
You might choose to use the second alternative because it corresponds more closely with counting. On the other hand, the Linear Indexed Traversal pattern is a very common, and important, pattern because it is used to step through one of the most common data structures.
for (int i = 0; i <= nbrPlayers; i++) INCORRECT!
{ dealHand();
}
your action will be repeated nbrPlayers+1 times, not nbrPlayers times. If,
on the other hand, you write
for (int i = 1; i < nbrPlayers; i++) INCORRECT!
{ dealHand();
}
your action will be repeated nbrPlayers-1 times. To avoid these kinds of mistakes
many people find it easiest to consistently use the Linear
Indexed Traversal pattern
(i = 0; i < N; i++)
Another alternative is to count down rather than counting up. This alternative and other types of Repetition are described in the complete Repetition Pattern document.
Exercise: Simulate Fish Moving Forward
|
In the Prompted Input pattern, we print a prompt that describes the type of input required from the user and then read in the desired value. A well-written and robust version of this pattern verifies that the input actually provided by the user matches the expectations of the program. For example, if we prompt the user to provide an integer, we should verify that the input is, in fact, a valid integer and not a string, floating point number, or other type.
Exercise: Allow User to Control Simulation Steps
|
Exercise: Fish Doing Their Own Thing
|