257
Summing is a looping operation that keeps a running total of certain values. It is
like counting in that the variable that holds the sum is initialized outside the loop.
The summing operation, however, adds up unknown values; the counting operation
adds a constant to the counter (or decrements it by a constant) each time.
When you design a loop, there are seven points to consider: how the termination
condition is initialized, tested, and updated; how the process in the loop is
initialized, performed, and updated; and the state of the code upon loop exit. By an-
swering the checklist questions, you can bring each of these points into focus.
To design a nested loop structure, begin with the outermost loop. When you get to
the point where the inner loop must appear, make it a separate module and come
back to its design later.
Objects have state. If an object’s initial state can be changed by any of its
methods, it is said to be mutable. Otherwise, it is immutable. When a mutable object
is passed as an argument, changes made by the method to its corresponding param-
eter can affect the state of the object. Immutable objects are protected from such
changes.
Quick Check
1.If an application will have input consisting of 1,000 integer numbers, is interac-
tive input appropriate? (pp. 212–213)
2.What does a constructor for an input file do? (pp. 215–216)
3.What does the following series of statements write on the filefileOut?
(pp. 216–218)
fileOut.print(’W’);
fileOut.print( 88 );
fileOut.print(" What comes next?");
4.What is the statement that reads in a string and stores the integer equivalent
into number? (pp. 217–218)
5.Write the first line of a whilestatement that loops until the value of the boolean
variable donebecomes true. (pp. 221–222)
6.What are the four parts of a count-controlled loop? (pp. 223–225)
7.Should you use a priming read with an EOF-controlled loop? (pp. 227–229)
8.How is a flag variable used to control a loop? (pp. 232–233)
9.What is the difference between a counting operation in a loop and a summing
operation in a loop? (pp. 229–233)
10.What is the difference between a loop control variable and an event counter?
(pp. 229–233)