COMPUTER
COMPUTER
The loop sentinel value is q and is checked in the logical expression letter !=
'q'. The while loop in Example 2 uses a Priming Read--an input statement that
appears before the while loop is entered. We say that line 23 primes the while
loop. The variable letter can have a value before the while condition could be
evaluated.
The loop counter is counter and is initialized in line 11. It will be used to count
the iterations of the loop and to serve as the value summed. The while loop's
logical expression is counter < 100, and the loop body consists of lines 14 through
17. The loop counter is incremented in line 16. Lines 10,11,12 and 18 are not part
of the while loop statement. Lines 11 and 12 initialize variables (the counting and
accumulating variables) that will be used in the while loop and line 17 displays
information calculated using the while loop. Lines 15 and 16 will only be executed
when the logical expression counter < 100 evalutes to true.
Flag-Controlled WHILE Loop A flag is a boolean variable (only can be set to true or
false). If a flag is used in a while loop to control the loop execution, then the
while loop is referred to as a flag-controlled while loop. Hence, the end-of-file-
controlled while loop is a special case of a flag-controlled while loop.
Example 4. The following is a flag-controlled while loop code segment. Line numbers
are given for reference only.
The loop logical expression ( !done ) indicates the loop should be executed as long
as done is false.