Selection And Iteration Code Guides: Avoiding Infinite Loops In Exam Questions (Copy)
Avoiding Infinite Loops In Exam Questions
What An Infinite Loop Is
- An infinite loop is a loop that never stops executing
- Happens when:
- The loop condition never becomes FALSE (WHILE)
- The exit condition never becomes TRUE (REPEAT UNTIL)
- In exam questions:
- Even one infinite loop = logic considered unsafe
- Can cost method marks even if output looks correct
Core Examiner Rule (Non-Negotiable)
- Every loop must have a clear and reachable exit condition
- Examiners look for:
- A variable in the condition
- That variable being updated correctly inside the loop
- Update moving the logic towards termination
Main Causes Of Infinite Loops (Big Picture)
| Cause | Loop Type | Why It Becomes Infinite |
|---|---|---|
| Condition variable never changes | WHILE / REPEAT | Condition never flips |
| Wrong comparison operator | WHILE / REPEAT | Condition always TRUE/FALSE |
| Counter update missing | All | No progress towards exit |
| Wrong STEP direction | FOR | Counter moves away from end |
| Sentinel never reached | WHILE / REPEAT | Exit value never met |
| Logical operator misuse (AND / OR) | WHILE / REPEAT | Condition permanently satisfied |
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
Infinite Loops In FOR Loops (Yes, They Happen Too)
Cause 1: Wrong STEP Direction
- Example (infinite)
FOR i ← 10 TO 1 STEP 1
OUTPUT i
NEXT i
- Why infinite
iincreases instead of decreasing
- Correct
FOR i ← 10 TO 1 STEP -1
OUTPUT i
NEXT i
Cause 2: Changing Counter Inside Loop
- Example (unpredictable / unsafe)
FOR i ← 1 TO 10
i ← i - 1
NEXT i
- Why dangerous
- Counter never reaches end
- Exam rule
- Never manually change FOR counter
Cause 3: End Value Never Reached
- Example
FOR i ← 1 TO 10 STEP 2
OUTPUT i
NEXT i
- Is this infinite?
- No, but end value
10is never reached
- No, but end value
- Examiner note
- Loop still terminates safely
- Do not confuse with infinite loops
Infinite Loops In WHILE Loops (Most Common Exam Error)
Cause 1: Control Variable Never Updated
- Example (classic infinite loop)
i ← 1
WHILE i <= 5 DO
OUTPUT i
ENDWHILE
- Why infinite
inever changes
- Correct
i ← 1
WHILE i <= 5 DO
OUTPUT i
i ← i + 1
ENDWHILE
Cause 2: Condition Always TRUE
- Example
WHILE 1 = 1 DO
OUTPUT "Hello"
ENDWHILE
- Why infinite
- Condition is permanently TRUE
- Examiner warning
- Treated as invalid logic
Cause 3: Wrong Comparison Operator
- Example
i ← 1
WHILE i >= 1 DO
OUTPUT i
i ← i + 1
ENDWHILE
- Why infinite
ikeeps increasing- Condition never becomes FALSE
- Correct
WHILE i <= 5 DO
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
Infinite Loops In REPEAT UNTIL Loops
Cause 1: Exit Condition Never Becomes TRUE
- Example
REPEAT
OUTPUT "X"
UNTIL 2 < 1
- Why infinite
- Condition is permanently FALSE
- Correct
- Condition must depend on a changing variable
Cause 2: Variable Not Updated Inside Loop
- Example
count ← 0
REPEAT
OUTPUT count
UNTIL count = 5
- Why infinite
countnever changes
- Correct
count ← 0
REPEAT
OUTPUT count
count ← count + 1
UNTIL count = 5
Cause 3: Wrong Logical Operator
- Example (always FALSE)
UNTIL num < 0 AND num > 10
- Why infinite
- Condition can never be TRUE
- Correct
UNTIL num < 0 OR num > 10
(if stopping on invalid input)
Sentinel-Controlled Infinite Loop Errors
Sentinel Loop Pattern (Safe)
INPUT num
WHILE num <> -1 DO
<process>
INPUT num
ENDWHILE
Common Infinite Version
INPUT num
WHILE num <> -1 DO
<process>
ENDWHILE
- Why infinite
numnever changes
- Fix
- Re-input inside loop
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
Logical Operator Traps (AND / OR)
AND Trap
WHILE num >= 0 AND num <= 100 DO
- Loop runs only while input is valid
- If no re-input inside loop → infinite
OR Trap
WHILE num < 0 OR num > 100 DO
- Correct for validation only if input is updated
Early-Exit Logic That Prevents Infinite Loops
Using Flags Safely
found ← FALSE
index ← 1
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
- Why safe
- Two independent exit routes
- Examiner likes
- Defensive logic
Dry-Run Technique To Detect Infinite Loops
Step-by-Step Safety Check
- Identify loop type
- Locate loop condition
- Identify all variables in condition
- Check:
- Are they initialised?
- Are they updated?
- Do updates move towards exit?
Examiner Red Flags (Instant Mark Loss)
- Loop condition uses constant values only
- Loop variable updated outside loop
- No update for condition variable
- Counter moves in wrong direction
- Sentinel not re-input
Safe Loop Writing Checklist (Memorise This)
- Loop condition uses variable(s)
- Variable(s) initialised before loop
- Variable(s) updated inside loop
- Update moves towards termination
- Logical operators make sense
- Loop type matches problem
Mini Exam Practice: Infinite Or Safe?
Practice 1
i ← 1
WHILE i < 5 DO
OUTPUT i
i ← i + 1
ENDWHILE
- Safe
Practice 2
REPEAT
OUTPUT "A"
UNTIL 5 = 10
- Infinite
Practice 3
FOR i ← 1 TO 10 STEP -1
OUTPUT i
NEXT i
- Infinite
Practice 4
INPUT num
WHILE num <> 0 DO
INPUT num
ENDWHILE
- Safe
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 Exam Rules To Remember
- No update = infinite
- Wrong direction = infinite
- Constant condition = infinite
- Sentinel not re-input = infinite
- If you can’t explain how it stops → it probably doesn’t
