Sentinel Loop C++



In computer programming, a sentinel value (also referred to as a flag value, trip value, rogue value, signal value, or dummy data)[1] is a special value in the context of an algorithm which uses its presence as a condition of termination, typically in a loop or recursive algorithm.

The sentinel value is a form of in-band data that makes it possible to detect the end of the data when no out-of-band data (such as an explicit size indication) is provided. The value should be selected in such a way that it is guaranteed to be distinct from all legal data values since otherwise, the presence of such values would prematurely signal the end of the data (the semipredicate problem). A sentinel value is sometimes known as an 'Elephant in Cairo,' due to a joke where this is used as a physical sentinel. In safe languages, most sentinel values could be replaced with option types, which enforce explicit handling of the exceptional case.

Examples[edit]

Some examples of common sentinel values and their uses:

Looping in C The while statement is like the if statement in that it tests a condition. Just like an if statement, the condition in a while statement can be an expression of any simple data type. In C, any nonzero value is coerced to type Boolean true, and zero is coerced to type Boolean false. For example, in a sentinel-controlled loop, the prompts requesting data entry should explicitly remind the user what the sentinel value is. Floating-Point Number Precision and Memory Requirements Variables of type float represent single-precision floating-point numbers and have seven significant digits on most 32-bit systems.

  • Null character for indicating the end of a null-terminated string
  • Null pointer for indicating the end of a linked list or a tree.
  • A set most significant bit in a stream of equally spaced data values, for example, a set 8th bit in a stream of 7-bit ASCII characters stored in 8-bit bytes indicating a special property (like inverse video, boldface or italics) or the end of the stream
  • A negative integer for indicating the end of a sequence of non-negative integers
  1. 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.
  2. The break statement in C programming has the following two usages − When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement (covered in the next chapter).

Variants[edit]

A related practice, used in slightly different circumstances, is to place some specific value at the end of the data, in order to avoid the need for an explicit test for termination in some processing loop, because the value will trigger termination by the tests already present for other reasons. Unlike the above uses, this is not how the data is naturally stored or processed, but is instead an optimization, compared to the straightforward algorithm that checks for termination. This is typically used in searching.[2][3]

C++ Counting Loop

For instance, when searching for a particular value in an unsorted list, every element will be compared against this value, with the loop terminating when equality is found; however, to deal with the case that the value should be absent, one must also test after each step for having completed the search unsuccessfully. By appending the value searched for to the end of the list, an unsuccessful search is no longer possible, and no explicit termination test is required in the inner loop; afterward, one must still decide whether a true match was found, but this test needs to be performed only once rather than at each iteration.[4]Knuth calls the value so placed at the end of the data, a dummy value rather than a sentinel.

Examples[edit]

Array[edit]

Loops in c++ programming notes

For example, if searching for a value in an array in C, a straightforward implementation is as follows; note the use of a negative number (invalid index) to solve the semipredicate problem of returning 'no result':

However, this does two tests at each iteration of the loop: whether the value has been found and whether the end of the array has been reached. This latter test is what is avoided by using a sentinel value. Assuming the array can be extended by one element (without memory allocation or cleanup; this is more realistic for a linked list, as below), this can be rewritten as:

Sentinel controlled loop in c

The test for i < len is still present, but it has been moved outside the loop, which now contains only a single test (for the value), and is guaranteed to terminate due to the sentinel value. There is a single check on termination if the sentinel value has been hit, which replaces a test for each iteration.

It is also possible to temporarily replace the last element of the array by a sentinel and handle it, especially if it is reached:

Sentinel Value Do While Loop C++

See also[edit]

References[edit]

Sentinel Loop C++C++Sentinel
  1. ^Knuth, Donald (1973). The Art of Computer Programming, Volume 1: Fundamental Algorithms (second edition). Addison-Wesley. pp. 213–214, also p. 631. ISBN0-201-03809-9.
  2. ^Mehlhorn, Kurt; Sanders, Peter (2008). Algorithms and Data Structures: The Basic Toolbox 3 Representing Sequences by Arrays and Linked Lists(PDF). Springer. ISBN978-3-540-77977-3. p. 63
  3. ^McConnell, Steve (2004). Code Complete (2nd ed.). Redmond: Microsoft Press. p. 621. ISBN0-7356-1967-0.
  4. ^Knuth, Donald (1973). The Art of Computer Programming, Volume 3: Sorting and searching. Addison-Wesley. p. 395. ISBN0-201-03803-X.

C++ Sentinel While Loop

Retrieved from 'https://en.wikipedia.org/w/index.php?title=Sentinel_value&oldid=987671612'