MINI-LAB: STAYING IN BOUNDS AND ADDING COLOR
Using Conditional Statements
This set of Mini-Lab Exercises includes the following:
In our current program, there is nothing to keep a fish from moving forward right out of the aquarium. This does not make sense. We can use the Whether or Not selection pattern to change direction when a fish gets to the wall.
You are in a situation in which some action may be appropriate or inappropriate depending on some testable condition. There are no other actions to do instead of this one. You don't need to repeat the action, only to decide whether or not it should be done.
Therefore, use an IF statement without an ELSE part, expressing the test as a Positive Condition if possible.
if (<condition>) <action>Note: Conditions are sometimes called guards. The condition guards the command and only permits it to be executed when the guard is true.
For example: in a power plant simulation, it may be necessary to shut down a generator when it overheats.
if (measuredHeat() > subBoilThreshold)
{ shutDownGenerator();
}
This selection pattern and others are described more fully in the complete Selection Pattern.
Most people can read a positive statement more effectively than a negative one. You want your code to be as readable as possible.
Therefore, when writing conditions, express them positively whenever possible.
For example, suppose you have a robot simulation in which the robot must move but must also contend with obstructions in the path. Suppose you have a boolean test as a primitive in a robot language:
boolean frontIsBlocked();Suppose that you want to move if possible, but turn Left instead if it is impossible to move forward. The following are equivalent:
if (frontIsBlocked()) turnLeft(); else move(); if ( ! frontIsBlocked()) move(); else turnLeft();The first version is more readable and is preferred. It expresses a positive condition.
ExerciseResearch the AquaFish partial interface to discover how to know whether a fish is up against a wall and how to make a fish change direction. Modify your main function to have each fish change direction before moving forward if it is about to hit a wall. |
Therefore, use a single IF statement with an ELSE part, expressing the test as a Positive Condition if possible.
if (<condition>) <one action> else <another action>For example a student may pass or fail an exam depending on the value of the numeric grade.
if ( numericGrade > 60 )
{ output ("passing");
}
else
{ output("failing");
}
If you try to apply Whether or Not (twice)
to this case you will find yourself needing to write the negation of the
condition.
if ( numericGrade > 60 )
{ output ("passing");
}
if ( numericGrade <= 60)
{ output("failing");
}
This is both wasteful of computer time and very error prone. If the problem
changes a bit in the future and you change one of the conditions, it is
easy to forget to change the other. Alternative Action makes it unnecessary
to repeat the condition for the else part.
This selection pattern and others are described more fully in the complete Selection Pattern.
ExerciseResearch the AquaFish partial interface to discover how to specify the color of a fish as you create it. Also look to see how you would find out a fish's color. Edit your main function to make each of your fish either red or blue as you construct them.Then edit the Display Show function that displays a single fish. Set the color of the pen to RED or BLUE depending on the color of the fish. Note that you can't just pass the fish's color to the SetBrush function, because fish colors are stored as strings, whereas the argument to the SetBrush function is an integer constant. Instead, you should use the Alternative Action Selection pattern to set the pen color depending on the color of the fish. |
You are in a situation in which you need to choose exactly one of several possible actions. Each action is guarded by a separate condition, and after you find one condition true you want to execute its associated action and at that point you want to finish.
You want the code layout to be pleasing to both the eye and the mind. You want a structure that is easy to read and understand.
Therefore, write a sequence of IF's, where each IF but the last has an ELSE part that consists entirely of another IF. For example,
int participants = myParty.size();
if (participants > 15000)
{ rentTheSuperdome();
}
else if (participants > 1500)
{ rentTheCivicCenter();
}
else if (participants > 150)
{ rentATent();
}
else
{ rentAMovie(); // default case, no party at all.
}
The formatting, with else and if on the same line, makes
it clear that this is a Sequential Choice and not a sequence of independent
Whether or Not applications. Do not
indent the subsequent
else parts or it will look like Independent Choice
(see the complete Selection
Pattern) and you will
also give up valuable horizontal real estate. Some languages have a special
keyword (elsif) to handle this case.
ExerciseChoose several colors (more than two) to use for fish in the aquarium. You may wish to look at the colors.h file in the CMU Graphics Library to see which colors you can display. Edit your main function to make each of your fish a different color.Now edit the Display Show function to set the pen color to the color of the fish, using the Sequential Choice Selection pattern. Remember that fish colors are stored as strings, whereas the argument to the SetBrush function is an integer constant. |