You are on page 1of 2

Sentinen tel

An event-controlled while loop may use a special data value, sentinel, in


conjunction with the logical expression to signal the loop exit. As long as the
sentinel value does not meet the logical expression (specified data value), the
loop is executed. This looping construct is referred to as a sentinel-controlled
while loop.

Example 2. The following is a sentinel-controlled while loop code segment. Line


numbers are given for reference only. The while loop reads non-white space until
the character q is read. All non-white space characters except the q are counted.

20 char letter; // storage for letters read


21 int counter; // counts the number of letters
22
23 cin >> letter; // read the first letter
24 counter = 0; // initialize the counter
25 while ( letter != 'q') // loop until 'q' is read
26 {
27 ++counter; // increment letter counter
28 cin >> letter; // read next letter
29 }
30 cout << "The number of letters read is " << counter << endl;

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.

Count-Controlled WHILE Loop


The count-controlled while loop uses a loop control variable in the logical
expression. The loop control variable should be initialized before entering the
while loop, and a statement in the body of the while loop should
increment/decrement the control variable.

Example 1. The following is a count-controlled while loop code segment. Line


numbers are given for reference only. This example also makes use of the counting
and summing techniques given above to find the sum of the first 99 positive
integers.

10 int counter, sum; // declare loop counter and sum


11 counter = 1; // initialize the loop counter
12 sum = 0; // initialize the sum
13 while ( counter < 100) // iterate the loop 99 times
14 {
15 sum = sum + counter; // add the value of counter to the existing sum
16 ++counter; // increment the counter
17 }
18 cout << "The sum is " << sum << endl; // Display the sum

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.

40 int sum; // the sum of the numbers read


41 bool done; // used to control the while loop
42 int number; // the number read
43
44 sum = 0; // initialize the sum
45 done = false; // initialize the boolean variable done
46 while ( !done ) // loop while not done
47 {
48 cin >> number; // read next number
49 if ( number > 0 ) // sum number if positive
50 sum = sum + number; // sum the number
51 else
52 done = true; // terminate the loop
53 }

The loop logical expression ( !done ) indicates the loop should be executed as long
as done is false.

You might also like