Introducing Loops and Complex Conditions
This set of Mini-Lab Exercises is part of 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.
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.
The Java for
statement (also known as a for
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> )
<repeated action>
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.
The counting
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).
For example,
consider a code fragment that deals a single card to each player in a
card game. Both examples below repeat the dealCard()
action 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 (<
)
operator.
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++ )
The conditional expression in the for
statement can be
written as either a positive or negative expression.
Positive Conditions
are generally easier to read, though.
Exercise
|
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.
Exercise
|