Page 71 - Introduction to Programming with Java: A Problem Solving Approach
P. 71
updated. This assignment overwrites the old value and replaces it with a new value. Thus, when it computes count 1, the computer uses the old value of count. Then (in the subsequent assignment) it changes the value in count to the new value.
In practice, all loops should have some kind of termination. That is, they should stop executing at some point. A loop that counts up normally uses a maximum value as a termination condition. For example, Figure 2.9’s loop continues as long as count is less than or equal to 100, and it terminates (stops looping) when count reaches 101. A loop that counts down normally uses a minimum value as a termination condi- tion. For example, a loop might start with count equal to 100 and continue as long as count is greater than zero. Then the loop would terminate when count reached zero.
When a loop’s condition compares a counter variable to a maximum value, the question often arises about whether to use “less than or equal to” or just “less than.” Likewise, when a loop’s condition compares a counter variable to a minimum value, the question often arises about whether to use “greater than or equal to” or just “greater than.” There are no absolute answers to those questions. Sometimes you’ll need to do it one way, and sometimes you’ll need to do it the other way—it depends on the situation. For example, look again at the decision condition in Figure 2.9’s Happy birthday algorithm. Suppose you used “less than.” Then, when count equaled 100, you would quit before printing the last (100th) “Happy birthday!” Therefore, in this case you should use “less than or equal to.” If you mistakenly used “less than,” that would be an off- by-one error. Such errors are called “off by one” because they occur when you execute a loop one more time than you should or one less time than you should. To avoid off-by-one errors, you should always double check the borderline cases for your algorithms’ loops.
The while Loop
Most popular programming languages have several different types of loops. Although it may be awkward, theoretically, there’s always a way to convert any one type of loop to any other type of loop. So, for simplic- ity and brevity, in this discussion of algorithms we’ll consider only one type of loop and look at the other types when we get into the details of the Java language. The type of loop we’ll consider now is a very popu- lar one, the while loop, which has this format:
while <condition> <statement(s)>
This format should look familiar because it’s similar to the if statement’s format. The condition is at the top, and the subordinate statements are indented. The subordinate statements, which are inside the loop, are called the loop’s body. The number of times that a loop repeats is called the number of iterations. It’s possible for a loop to repeat forever, which is called an infinite loop. It’s also possible for a loop to repeat zero times. There’s no special name for the zero-iteration occurrence, but it’s important to be aware that this sometimes happens. For an example, let’s see how Figure 2.9’s Happy birthday flowchart looks when it’s presented as pseudocode with a while loop. This is shown in Figure 2.10.
Apago PDF Enhancer
2.8 Loops 37
set count to 1
while count is less than or equal to 100
print “Happy birthday!” set count to count + 1
Figure 2.10 Pseudocode for another Happy Birthday algorithm