|
|
|
|
|
|
SENTINEL VALUE
In computer programming, a sentinel value (also referred to as a flag value or signal value) is a special value that is used to terminate a loop that processes structured (especially sequential) data. The value should be selected in such a way that it will not be confused with legal data values.
Below are some examples of common sentinel values and their uses:
Sentinel values are often used when searching for something.[1] The sentinel value will signal that the object is found and that the loop can stop iterating. An example of a sentinel value used to terminate a loop is in the following psuedocode:
found = false;
i= 0;
while ((found == false) && (i < NumberOfObjects)) {
if ( (objects[i] == WhatWeAreLookingFor) ) {
found = true;
}
else {
i++;
}
}
The sentinel value in this example is the boolean "found". Once the object is located, found is true, and we break out of the loop. Without the sentinel value we would have to iterate through the whole loop even after we have found what we are looking for.
See also
Notes
- ^ McConnell, Steve. "Code Complete" Edition 2 Pg. 621 ISBN - 0-7356-1967-0
|
|
|
|
|
|
|