In computer programming, a sentinel value (also referred to as a flag value, trip value, rogue value, signal value, or dummy data) 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 uses of sentinel values could be replaced with option types, which enforce explicit handling of the exceptional case.
Some examples of common sentinel values and their uses:
In the morning by the sea
As the fog clears from the sand
I have no money in my hand
I have no home, I have no land
But it doesn’t trouble me
As I lay beside the fire
I am easy to inspire
There is little I require
I wasn’t yours and you weren’t mine
Though I’ve wished from time to time
We had found a common ground
Your voice was such a welcome sound
How the emptiness would fill
With the waves and with your song
People find where they belong
Or keep on
Through the never-ending maze
Where the way is seldom clear
There is no map or compass near
I drive a ship I cannot steer
Through the bleak and early morn
Where a stronger will is sworn
Where the moments move so slow
And seem to never let you go
When my hands are old and ache
And my memory flickers dim
And my bones don’t hold my skin
There’s no place I haven’t been
I recall the days were few
That is all that I can do
Feel the carvings in the tree