0% found this document useful (0 votes)
35 views

Cin Age // Read in Age: // Process It

The document discusses sentinel-controlled loops. It provides an example of using a sentinel value of -1 to mark the end of a list of age values being read from input. A priming read is recommended to read the first value before the while loop. This ensures the first value is processed, as it would otherwise be skipped if it was the sentinel value. The code example with a priming read correctly processes all age values until it reads the sentinel -1, without missing the first value.

Uploaded by

S.B.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Cin Age // Read in Age: // Process It

The document discusses sentinel-controlled loops. It provides an example of using a sentinel value of -1 to mark the end of a list of age values being read from input. A priming read is recommended to read the first value before the while loop. This ensures the first value is processed, as it would otherwise be skipped if it was the sentinel value. The code example with a priming read correctly processes all age values until it reads the sentinel -1, without missing the first value.

Uploaded by

S.B.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

2.

Event controlled Loops


Here termination depends on some event occurring in the loop body.
2a. Sentinel controlled loops
We often use lops to read and process long lists of data. A sentinel
(trailer value) is used to mark the end of the data. This, a sentinel must
be a value that will never appear as a typical value in the data.
while (age != -1)
{
cin >> age; // read in age
:

// process it

}
The sentinel value for age is -1. Since age is a variable, it must be
previously declared.
How about? Int age = -99
This will work but a more elegant approach is to use a priming read.
This means to read the first data value immediately before the while
loop.
The above code, using a priming read, now looks like:
int age;
cin >> age;

//priming read

while(age != -1)
{
cin >> age;

// get next value


// process it

}
If the first value of age is -1, then we dont enter the while loop. This
part is correct.
However, the code is incorrect as it does not process the first age
value.
cin >> age;

// priming read (get 1st value)

while (age != -1)


while (age != -1)
{
--- // process
cin >> age; // get next value
Sentinel While-Loop Template
priming read (get 1st value)
while (varName !=..
whats your while expression, whats varName .

Write a code to read and echo the characters from one line of
than input file. (quiz #2)
How do you get the next character from a buffer? we use dot get.
What are we going to store it in? a char variable.
While (____ != )/n
Whats the process? Simply print the value
After done processing? get the next char value, we are going to use
the .getvalue
What goes next? close that bracket.
*read and echo simply means read and print.
There are 4 kinds of loop?
1ST QUESTION: Do you know the number times the loop will iterate.
2nd QUESTION: Is there a special value marking the end of the data?
(you stop looping when you read /endl)
Sentinel loop
Read

You might also like