< previous page page_275 next page >

Page 275
The condition is updated by increasing the value of the counter by 1 for each iteration. (Occasionally, you may come across a problem that requires a counter to count from some value down to a lower value. In this case, the initial value is the greater value, and the counter is decremented by 1 for each iteration.) So, for count-controlled loops that use an iteration counter, these are the answers to the questions:
Initialize the iteration counter to 1.
Increment the iteration counter at the end of each iteration.
If the loop is controlled by a variable that is counting an event within the loop, the control variable usually is initialized to 0 and is incremented each time the event occurs. For count-controlled loops that use an event counter, these are the answers to the questions:
Initialize the event counter to 0.
Increment the event counter each time the event occurs.
Sentinel-Controlled Loops
In sentinel-controlled loops, a priming read may be the only initialization necessary. If the source of input is a file rather than the keyboard, it also may be necessary to open the file in preparation for reading. To update the condition, a new value is read at the end of each iteration. So, for sentinel-controlled loops, we answer our questions this way:
Open the file, if necessary, and input a value before entering the loop (priming read).
Input a new value for processing at the end of each iteration.
EOF-Controlled Loops
EOF-controlled loops require the same initialization as sentinel-controlled loops. You must open the file, if necessary, and perform a priming read. Updating the loop condition happens implicitly; the stream state is updated to reflect success or failure every time a value is input. However, if the loop doesn't read any data, it will never reach EOF, so updating the loop condition means the loop must keep reading data.
Flag-Controlled Loops
In flag-controlled loops, the Boolean flag variable must be initialized to TRUE or FALSE and then updated when the condition changes.
Initialize the flag variable to TRUE or FALSE, as appropriate.
Update the flag variable as soon as the condition changes.
In a flag-controlled loop, the flag variable essentially remains unchanged until it is time for the loop to end. Then the code detects some condition

 
< previous page page_275 next page >