In computer programming, a sentinel value 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 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. 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 uses of sentinel values could be replaced with option types, which enforce explicit handling of the exceptional case.
Examples
Some examples of common sentinel values and their uses:
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 or the end of the stream
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. 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; afterwards one must still decide whether a true match was found, but this test needs to be performed only once rather than at each iteration. Knuth calls the value so placed at the end of the data a dummy value rather than a sentinel.
However, this does two tests at each iteration of the loop: whether the value has been found, and then 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, this can be rewritten as: int find
The test for i < len is still present, but it has been moved outside the loop, which now contains only a single test, and is guaranteed to terminate, due to the sentinel value. On termination, there is a single check 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 specially if it is reached: int find