views:

447

answers:

5

Can anyone tell me what is sentinel while loop in C++? Please give me an example using sentinel while loop.

A: 

It's usually a boolean (true or false) variable that is set to false when a condition is not satisfied and set to true when it is. Then we can loop so long as the sentinel is false.

Example:

bool acceptedAnswer = false;
while (acceptedAnswer == false)
{
  refreshPage();
  if (hasBeenAccepted())
  {
     acceptedAnswer = true;
  }
}
JRL
That is incorrect. That is simply a termination condition, or a "guard". As others have said, a sentinel is a special value in an iteration that signals to end the loop.
Peter Alexander
+10  A: 

A "sentinel" in this context is a special value used to indicate the end of a sequence. The most common sentinel is \0 at the end of strings. A "sentinel while loop" would typically have the form:

while (Get(input) != Sentinel) {
  Process(input);
}
MSalters
+2  A: 

A sentinel is a special value, e.g. boolean value, extremely big or small. It is used to determine when to stop the loop.

A good example is in the implementation of merge sort, e.g. read page 4 of http://www.cs.princeton.edu/courses/archive/spr07/cos226/lectures/04MergeQuick.pdf.

Yin Zhu
+1  A: 

As an addendum to JRL's answer..

Please note that there was nothing wrong with asking this question, but in the future you may find more immediate help by going to dictionary.com and looking up words you don't know.

edit: in this case, the dictionary leaves nothing for you to think on. Here is definition 3 :)

3 Also called  tag.  Computers. a symbol, mark, or other labeling device indicating the beginning or end of a unit of information.
San Jacinto
A: 

A sentinal is a special value in a list of items that will always cause the iterator to stop.

For example, the zero terminator in an ASCIIZ string acts as a sentinal.

Linked lists often have NULL pointers and so.

Will