Programming and Problem Solving with Java

(やまだぃちぅ) #1
5.2 Looping | 241

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 lower-level module:


1.What condition 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
output file.
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 code 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 full algorithm:

Read line from dataFile
whileNOT EOF
Set starCount to the integer equivalent of the string in line
Set loopCount = 1
whileloopCount <= starCount
Print '*' on outFile
Increment loopCount
Output newline on outFile
Read line from dataFile
Print "End"

Read line from dataFile
whileNOT EOF
Set starCount to the integer equivalent of the string in line
Print starCount asterisks
Output newline on outFile
Read line from dataFile
Print "End"
Free download pdf