< previous page page_281 next page >

Page 281
Here's a sample run of the program. The user's input is in color. Again, the symbols <EOF> denote the end-of-file keystrokes pressed by the user (the symbols would not appear on the screen).
3
***
1
*
<EOF>
Goodbye
Because starCount and loopCount are variables, their values remain the same until they are explicitly changed, as indicated by the repeating values in Table 6-1. Only when the test is made are the values of the logical expressions cin and loopCount <= starCount shown. We've used dashes in those columns at all other times.
Designing Nested Loops
To design a nested loop, we begin with the outer loop. The process being repeated includes the nested loop as one of its steps. Because that step is more complex than a single statement, our top-down design methodology tells us to make it a separate module. We can come back to it later and design the nested loop just as we would any other loop.
For example, here's the design process for the code segment above:
1. What is the condition that ends the loop? EOF is reached in the input.
2. How should the condition be initialized? A priming read should be performed before the loop starts.
3. How should the condition be updated? An input statement should occur at the end of each iteration.
4. What is the process being repeated? Using the value of the current input integer, the code should print that many asterisks across one output line.
5. How should the process be initialized? No initialization is necessary.
6. How should the process be updated? A sequence of asterisks is output and then a newline character is output. There are no counter variables or sums to update.
7. What is the state of the program on exiting the loop? The cin stream is in the fail state (because the program tried to read past EOF), starCount contains the last integer read from the input stream, and the rows of asterisks have been printed along with a concluding message.

 
< previous page page_281 next page >