Page 164 - Introduction to Programming with Java: A Problem Solving Approach
P. 164

                130 Chapter 4 Control Statements
Note that the same variable, i, appears in all three components of the for loop header. That variable is given a special name. It’s called an index variable. Index variables in for loops are often, but not always, named i for “index.” Index variables often start at a low value, increment up, and then stop when they reach a threshold set by the condition component. But in the above example, the index variable does just the oppo- site. It starts at a high value (10), decrements down, and then stops when it reaches the threshold of 0. Let’s informally trace the example:
The initialization component assigns 10 to the index, i.
The condition component asks “Is i 􏰃 0?” The answer is yes, so execute the body of the loop.
Print 10 (because i is 10), and append a space.
Since you’re at the bottom of the loop, the update component decrements i from 10 to 9.
The condition component asks “Is i 􏰃 0?” The answer is yes, so execute the body of the loop.
Print 9 (because i is 9) and append a space.
Since you’re at the bottom of the loop, the update component decrements i from 9 to 8.
The condition component asks “Is i 􏰃 0?” The answer is yes, so execute the body of the loop. Repeat the previous printing and decrementing until you print 1.
.. .
After printing 1, since you’re at the bottom of the loop, decrement i from 1 to 0.
The condition component asks “Is i 􏰃 0?” The answer is no, so quit the loop, drop down to the first statement after the closing brace, and print “Liftoff!”
Alternatively, we could have implemented the solution with a while loop or a do loop. Why is the for Apago PDF Enhancer
loop preferable? With a while loop or a do loop, you’d need two extra statements to initialize and update the count variable. That would work OK, but using a for loop is more elegant.
Factorial Example
Now, let’s make sure you really understand how the for loop works by studying a formal trace of the sec- ond example mentioned at the start of this section—the calculation of a factorial. Figure 4.15 shows the factorial-calculation code listing and its associated trace. Note the input column in the top-left corner of the trace. You didn’t have input in Chapter 3’s trace examples, so input is worth mentioning now. When the program reads an input value, you copy the next input from the input column into the next row under the
variable to which the input is assigned. In this case, when you get to number copy the 4 from the input column to the next row in the number column.
=
stdIn.nextInt(), you
This trace shows that the 8, 10 sequence repeats three times, so there are indeed three iterations, as ex- pected. Suppose you entered number = 0. Does the program work for that extreme case? The loop header initializes int i=2 and then immediately tests to see if i<=number. Since this condition is false, the loop terminates before it starts, and the code prints the initial value of factorial, which is 1.0. That’s cor- rect, since 0 factorial does indeed equal 1.
What about the other extreme case—when the input value is very large? The factorial of a number in- creases much more rapidly than the number itself increases. If we had declared factorial to be of type int, then input values greater than 12 would cause the factorial variable to overflow, and the output value
         Little mistakes are better than big ones.
would be horribly wrong! That’s why we declared factorial to be of type double. A double has more precision than an int, and it gives approximately correct answers even when its precision is inadequate. This makes the program more robust, because it fails more gracefully. That is, when it fails, it fails just a little bit, not a lot.
  










































































   162   163   164   165   166