|
|
|
|
|
|
|
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 program upon loop exit. By answering 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 where the inner loop must appear, make it a separate module and come back to its design later. |
|
|
|
|
|
|
|
|
Looping programs often produce a large amount of output that can be easier to read in table form, with a heading for each column. Tables are easy to print using fieldwidth specifications. |
|
|
|
|
|
|
|
|
A loop invariant is a set of conditions that specify what must be true on loop entry, at the beginning of each iteration, and at loop exit in order for the loop to work properly. Writing out the loop invariant is a part of the verification process for programs that contain loops. |
|
|
|
|
|
|
|
|
The process of testing a loop is based on the loop invariant, the answers to the checklist questions, and the patterns it might encounter (for example, executing a single iteration, multiple iterations, an infinite number of iterations, or no iterations at all). |
|
|
|
|
|
|
|
|
1. Write the first line of a While statement that loops until the value of Boolean variable done becomes TRUE. (pp. 254257) |
|
|
|
|
|
|
|
|
2. What are the four parts of a count-controlled loop? (pp. 257259) |
|
|
|
|
|
|
|
|
3. Should you use a priming read with an EOF-controlled loop? (pp. 263264) |
|
|
|
|
|
|
|
|
4. How is a flag variable used to control a loop? (pp. 264265) |
|
|
|
|
|
|
|
|
5. What is the difference between a counting operation in a loop and a summing operation in a loop? (pp. 265269) |
|
|
|
|
|
|
|
|
6. What is the difference between a loop control variable and an event counter? (pp. 265269) |
|
|
|
|
|
|
|
|
7. What kind of loop would you use in a program that reads the closing price of a stock for each day of the week? (pp. 273277) |
|
|
|
|
|
|
|
|
8. How would you extend the loop in Question 7 to make it read prices for 52 weeks? (pp. 277283) |
|
|
|
|
|
|
|
|
9. With what kind of loop is the following invariant most likely associated? (pp. 305311) |
|
|
|
|
|
|
|
|
day indicates the number of the iteration that is about to be executed. |
|
|
|
|
|
|
|
|
10. How would you test a program that is supposed to count the number of females and the number of males in a data set? (Assume that females are coded with F in the data; males, with M.) (pp. 305311) |
|
|
|
|
|
|
|
|
Answers 1. while ( !done ) 2. The process being repeated, plus initializing, testing, and incrementing the loop control variable. 3. Yes. 4. The flag is set outside the loop; the While checks the flag; and an If inside the loop resets the flag when the termination condition occurs. 5. A counting operation increments by a fixed value with each iteration of the loop; a summing |
|
|
|
|
|