Please send questions to
st10@humboldt.edu .
* not all loops are count-controlled!
...some are EVENT-controlled -- continuing until some event happens;
...we're going to a discuss a particular kind of event-controlled
loop, that has an interesting pattern different from a
count-controlled loop: sentinel-controlled loop
* a loop where where data is accepted until a special
non-data value, called the SENTINEL, is read in;
* (it DOES help, for this loop, if there is a reasonable
choice of sentinel value, reasonable to distinguish from
"real" data;)
* a key point here: you don't WANT to treat that
sentinel, when encountered, as "real" data...
* that leads to the following pseudocode for the CLASSIC sentinel-controlled
loop (and you are expected to use this approach in this class):
get first piece of data
while (it isn't the sentinel)
{
handle data
get next piece of data
}
* see spicy_levels.cpp
(which uses a sentinel-controlled loop)
* biggest drawback here is when you don't happen to have a reasonable
sentinel value in a given setting...
* here is a variant for such a situation --
it isn't a sentinel-controlled loop, because the data read in
ISN'T determining when the loop stops --
BUT it has a similar structure:
(what I call the "question" approach...)
ask user if they want to go on
while the answer is yes
{
get the data
handle the data
ask user if they want to go on
}
* example of this approach:
see main function in spicy_q.cpp
LAB EXERCISE:
write a main function
that uses cheer (Week 11, Lecture 2)
and uses a sentinel-controlled loop to ask the user, over and over,
how many hips they'd like to see,
and then calls cheer for that many hips,
until the user enters a negative number of hips;
when you are done, or at 9:50, whichever comes first,
submit your .cpp and .h files using ~st10/131submit with a HW number of 13