Standard Algorithm & Logic Patterns: Sentinel Values In Algorithms (Copy)
Sentinel Values In Algorithms
What A Sentinel Value Is
- A sentinel value is a special value used to:
- Signal the end of input
- Stop a loop without knowing the number of inputs in advance
- The sentinel:
- Is not processed
- Is checked before or during loop termination
- Common examples:
-1,0,"END","X"(only if clearly stated)
Core Examiner Rule (High-Frequency)
- Sentinel value:
- Must be tested correctly
- Must not be included in calculations
- Using sentinel incorrectly = logic error + mark loss
When Sentinel Values Are Used
- Unknown number of inputs
- Continuous user input
- File-style or stream-style data
- Repeated input until “stop” condition occurs
Sentinel Value vs Fixed Loop Count
| Feature | Sentinel-Controlled | Fixed Loop |
|---|---|---|
| Number of inputs known | No | Yes |
| Loop ends when | Sentinel entered | Count reached |
| Flexibility | High | Limited |
| Risk | Forgetting to exclude sentinel | Wrong loop bounds |
Basic Sentinel-Controlled Loop (REPEAT UNTIL)
Standard Pattern (Exam-Preferred)
REPEAT
INPUT value
UNTIL value = sentinel
Important Rule
- This loop accepts sentinel once
- Processing must occur only if value is not sentinel
Sentinel With Processing (Correct Pattern)
Example: Sum Numbers Until -1
total ← 0
REPEAT
INPUT num
IF num <> -1 THEN
total ← total + num
ENDIF
UNTIL num = -1
OUTPUT total
Why This Is Correct
- Sentinel
-1:- Stops loop
- Is not added to total
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Sentinel Using WHILE Loop (Pre-Test)
When To Use
- Input already exists
- Sentinel checked before loop runs
Example: Count Inputs Until 0
count ← 0
INPUT num
WHILE num <> 0 DO
count ← count + 1
INPUT num
ENDWHILE
OUTPUT count
Examiner Note
- Initial input before loop
- Re-input inside loop
- Forgetting either causes errors
Sentinel With Counting And Accumulation (Very Common)
Example: Average Until -1
total ← 0
count ← 0
REPEAT
INPUT num
IF num <> -1 THEN
total ← total + num
count ← count + 1
ENDIF
UNTIL num = -1
IF count > 0 THEN
average ← total / count
OUTPUT average
ENDIF
Key Safety Check
count > 0prevents division by zero
Sentinel With Validation Combined
Example: Accept Positive Numbers Until -1
REPEAT
INPUT num
IF num > 0 THEN
OUTPUT num
ENDIF
UNTIL num = -1
Examiner Insight
- Sentinel check comes after processing condition
- Sentinel still terminates loop correctly
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Sentinel In Array Filling
Example: Store Values Until -1 Or Array Full
i ← 1
REPEAT
INPUT num
IF num <> -1 AND i <= 10 THEN
arr[i] ← num
i ← i + 1
ENDIF
UNTIL num = -1 OR i > 10
Why This Is Important
- Prevents array overflow
- Handles unknown input size safely
Sentinel With Strings
Example: Input Words Until “END”
count ← 0
REPEAT
INPUT word
IF word <> "END" THEN
count ← count + 1
ENDIF
UNTIL word = "END"
OUTPUT count
Sentinel In Nested Logic
Example: Stop Searching On Sentinel Or Found
found ← FALSE
REPEAT
INPUT num
IF num = target THEN
found ← TRUE
ENDIF
UNTIL num = -1 OR found = TRUE
IF found = TRUE THEN
OUTPUT "Found"
ELSE
OUTPUT "Not Found"
ENDIF
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
Common Sentinel Errors (Very High Frequency)
Error 1: Including Sentinel In Calculation
total ← total + num
UNTIL num = -1
- Sentinel added before stop
Error 2: Wrong Loop Condition
UNTIL num <> -1
- Loop stops immediately
- Condition reversed
Error 3: Forgetting Re-Input
WHILE num <> -1 DO
total ← total + num
ENDWHILE
- Infinite loop
Error 4: Processing Before Sentinel Check
- Sentinel processed accidentally
Sentinel Value Design Rules (Exam Language)
- “The sentinel value signals termination”
- “The sentinel is excluded from processing”
- “The loop continues until the sentinel is entered”
- “A sentinel allows unknown input size”
Sentinel vs Flag (Quick Comparison)
| Feature | Sentinel | Flag |
|---|---|---|
| Purpose | Stop input | Track condition |
| Value type | Data value | Boolean |
| Stops loop | Yes | Indirectly |
| Included in data | No | N/A |
Exam-Ready Sentinel Templates
REPEAT UNTIL Sentinel
REPEAT
INPUT value
IF value <> sentinel THEN
PROCESS value
ENDIF
UNTIL value = sentinel
WHILE Sentinel
INPUT value
WHILE value <> sentinel DO
PROCESS value
INPUT value
ENDWHILE
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change AS Level Computer Science Full Scale Course
One-Line Rules To Memorise
- Sentinel ends input, not data
- Never process sentinel
- Re-input inside WHILE loops
- REPEAT UNTIL is safest
- Sentinel logic must be crystal clear
