|
|
|
|
|
|
|
From the answers to these questions, we can write this much of the algorithm: |
|
|
|
|
|
|
|
|
Read starCount
WHILE NOT EOF
Print starCount asterisks
Output newline
Read starCount
Print Goodbye |
|
|
|
|
|
|
|
|
After designing the outer loop, it's obvious that the process in its body (printing a sequence of asterisks) is a complex step that requires us to design an inner loop. So we repeat the methodology for the corresponding lowerlevel module: |
|
|
|
|
|
|
|
|
1. What is the condition that ends the loop? An iteration counter exceeds the value of starCount. |
|
|
|
|
|
|
|
|
2. How should the condition be initialized? The iteration counter should be initialized to 1. |
|
|
|
|
|
|
|
|
3. How should the condition be updated? The iteration counter is incremented at the end of each iteration. |
|
|
|
|
|
|
|
|
4. What is the process being repeated? The code should print a single asterisk on the standard output device. |
|
|
|
|
|
|
|
|
5. How should the process be initialized? No initialization is needed. |
|
|
|
|
|
|
|
|
6. How should the process be updated? No update is needed. |
|
|
|
|
|
|
|
|
7. What is the state of the program on exiting the loop? A single row of asterisks has been printed, the writing marker is at the end of the current output line, and loopCount contains a value one greater than the current value of starCount. |
|
|
|
|
|
|
|
|
Now we can write the algorithm: |
|
|
|
|
|
|
|
|
Read starCount
WHILE NOT EOF
Set loopCount = 1
WHILE loopCount <= starCount
Print *
Increment loopCount
Output newline
Read starCount
Print Goodbye |
|
|
|
|
|
|
|
|
Of course, nested loops themselves can contain nested loops (called doubly nested loops), which can contain nested loops (triply nested loops), and so on. You can use this design process for any number of levels of nesting. |
|
|
|
|
|