< previous page page_283 next page >

Page 283
The trick is to defer details using the top-down methodologythat is, focus on the outermost loop first, and treat each new level of nested loop as a module within the loop that contains it.
It's also possible for the process within a loop to include more than one loop. For example, here's an algorithm that reads and prints people's names from a file, omitting the middle name in the output:
Read and print first name (ends with a comma)
WHILE NOT EOF
 Read and discard characters from middle name (ends with a comma)
 Read and print last name (ends at newline)
 Output newline
 Read and print first name (ends with a comma)
The steps for reading the first name, middle name, and last name require us to design three separate loops. All of these loops are sentinel-controlled.
This kind of complex control structure would be difficult to read if written out in full. There are simply too many variables, conditions, and steps to remember at one time. In the next two chapters, we examine the control structure that allows us to break programs down into more manageable chunksthe subprogram.
THEORETICAL FOUNDATIONS
The Magnitude of Work
There is usually more than one way to solve a problem. This may leave the programmer trying to choose the most efficient algorithm by deciding how much work is necessary to execute it.
Work A measure of the effort expended by the computer in performing a computation.
How do we measure the amount of work required to execute an algorithm? We use the total number of steps executed as a measure of work. One statement, such as an assignment, may require only one step; another, such as a loop, may require many

(text box continues on next page)

 
< previous page page_283 next page >