Dry Runs, Tracing & Logic Control: Detecting Infinite Loops In Given Code (Copy)
Detecting Infinite Loops In Given Code (Cambridge Standard – O Level 2210 + IGCSE 0478)
Why Infinite Loop Detection Is Tested In Paper 2
- Cambridge uses infinite loops to test:
- Logical control understanding
- Correct loop termination awareness
- Ability to reason about execution, not syntax
- Questions appear as:
- “Explain why this algorithm never stops”
- “Identify the error in the loop”
- Dry-run questions where execution never reaches OUTPUT
- Infinite loop detection is:
- A logic skill, not a programming-language skill
What Cambridge Defines As An Infinite Loop
- A loop that:
- Never reaches a condition that stops it
- Execution continues:
- Forever
- Or until external interruption (ignored in exam logic)
- From examiner perspective:
- If termination condition can never become FALSE (WHILE)
- Or never become TRUE (REPEAT UNTIL)
→ the loop is infinite
Core Principle For Detecting Infinite Loops
- A loop terminates only if:
- The condition can change
- And it changes in the correct direction
- If the condition:
- Never changes
- Or changes away from termination
→ infinite loop
Step 1: Identify The Loop Type First
- Cambridge pseudocode uses three loop types:
- FOR
- WHILE
- REPEAT UNTIL
- Infinite loops are:
- Rare with FOR
- Very common with WHILE and REPEAT UNTIL
FOR Loops And Infinite Loop Risk
Cambridge Rule
- FOR loops:
- Have fixed start
- Fixed end
- Fixed step (default +1 unless stated)
- FOR loops almost never cause infinite loops in exams
When FOR Loops Can Become Infinite (Rare Case)
- Incorrect STEP direction
Example:
- FOR i ← 1 TO 10 STEP -1
- OUTPUT i
- ENDFOR
Why infinite:
- i moves away from 10
- Termination condition never reached
Examiner Expectation
- Check:
- Start value
- End value
- STEP direction
WHILE Loops: Highest Infinite Loop Risk
WHILE Loop Structure
- WHILE condition DO
- statements
- ENDWHILE
- Condition is checked:
- Before every iteration
Core WHILE Loop Termination Rule
A WHILE loop terminates only if:
- Something inside the loop:
- Changes a variable in the condition
- And moves it toward FALSE
If not:
- Infinite loop
Infinite Loop Cause 1: Condition Variable Never Changes
Example
- count ← 0
- WHILE count < 5 DO
- OUTPUT count
- ENDWHILE
Why infinite:
- count is never updated
- count < 5 is always TRUE
Examiner Focus
- Look for:
- Assignment to count inside loop
- If missing:
- Loop never ends
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 O Level And IGCSE Computer Science Full Scale Course
Infinite Loop Cause 2: Condition Variable Updated In Wrong Direction
Example
- x ← 1
- WHILE x < 10 DO
- x ← x – 1
- ENDWHILE
Why infinite:
- x starts at 1
- x decreases
- x < 10 remains TRUE forever
Examiner Expectation
- Check:
- Initial value
- Update direction
- Comparison operator
Infinite Loop Cause 3: Condition Uses Wrong Operator
Example
- index ← 1
- WHILE index != 10 DO
- index ← index + 2
- ENDWHILE
Why infinite:
- index values: 1, 3, 5, 7, 9, 11, 13 …
- index never equals 10
- Condition never becomes FALSE
Examiner Trap
- Students assume “!=” always works
- Cambridge expects:
- Logical evaluation, not intent
Infinite Loop Cause 4: Multiple Conditions Blocking Termination
Example
- index ← 1
- WHILE index <= 10 AND found = FALSE DO
- index ← index + 1
- ENDWHILE
If:
- found is never set to TRUE
Why loop stops:
- index eventually exceeds 10
But if written as:
- WHILE index <= 10 OR found = FALSE DO
Why infinite:
- found = FALSE keeps condition TRUE forever
Examiner Focus
- AND vs OR logic
- One TRUE condition is enough for OR
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 O Level And IGCSE Computer Science Full Scale Course
REPEAT UNTIL Loops: Logical Reversal Traps
REPEAT UNTIL Structure
- REPEAT
- statements
- UNTIL condition
- Loop stops when:
- condition becomes TRUE
Infinite Loop Cause 5: Condition Never Becomes TRUE
Example
- count ← 0
- REPEAT
- count ← count + 1
- UNTIL count < 0
Why infinite:
- count starts at 0
- Increases
- count < 0 is always FALSE
Examiner Trap
- Students treat UNTIL like WHILE
- Condition logic is reversed
Infinite Loop Cause 6: Condition Variable Updated After UNTIL Check (Missing Update)
Example
- count ← 0
- REPEAT
- OUTPUT count
- UNTIL count = 5
Why infinite:
- count never changes
- Condition never becomes TRUE
Examiner Expectation
- Variable in UNTIL condition:
- Must change inside REPEAT
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 O Level And IGCSE Computer Science Full Scale Course
Infinite Loops Caused By Incorrect Placement Of Updates
Example
- index ← 1
- WHILE index <= 5 DO
- IF values[index] > 0 THEN
- OUTPUT values[index]
- ENDIF
- IF values[index] > 0 THEN
- ENDWHILE
Why infinite:
- index update missing
- index never changes
Examiner Focus
- Updates must occur:
- Every loop iteration
- Not only inside IF (unless guaranteed)
Infinite Loops In Nested Structures
Example
- i ← 1
- WHILE i <= 3 DO
- j ← 1
- WHILE j <= 2 DO
- OUTPUT i, j
- ENDWHILE
- i ← i + 1
- ENDWHILE
Why infinite:
- j is never incremented
- Inner loop never ends
- Outer loop never continues
Examiner Expectation
- Inner loop variables must update inside inner 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 O Level And IGCSE Computer Science Full Scale Course
Infinite Loops In Pre-Release Material
- Pre-release algorithms often:
- Use WHILE loops for searching
- Infinite loop risk occurs when:
- Search target is not guaranteed to exist
- Boundary condition is missing
Unsafe Search Example
- WHILE names[index] != target DO
- index ← index + 1
- ENDWHILE
Why infinite:
- target may not exist
- index exceeds bounds but loop condition still TRUE
Safe Cambridge Pattern
- WHILE index <= upperBound AND names[index] != target DO
- index ← index + 1
- ENDWHILE
How Cambridge Expects You To Explain Infinite Loops (Theory Questions)
Use structured explanation:
- Identify the loop type
- Identify the condition
- Identify the variable(s) in the condition
- Explain:
- Why the variable never changes
- Or why it moves away from termination
- Conclude:
- Condition never becomes false/true
Example Examiner-Style Explanation
- The WHILE loop never terminates because the variable
countused in the condition is never updated inside the loop. As a result, the conditioncount < 5remains true indefinitely, causing an infinite 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 O Level And IGCSE Computer Science Full Scale Course
Quick Detection Checklist (Use This In The Exam)
- What loop type is used?
- Which variable(s) control termination?
- Are those variables updated?
- Is the update inside the loop?
- Does the update move toward termination?
- Are logical operators correct (AND / OR)?
- Can the condition ever become FALSE (WHILE) or TRUE (REPEAT)?
If any answer is “no”:
- Infinite loop exists
Common Examiner Traps
- Forgetting index update
- Wrong comparison operator
- Wrong logical connector
- Assuming input guarantees termination
- Treating REPEAT UNTIL like WHILE
- Missing boundary conditions
Best-Practice Strategy For Paper 2
- Always simulate:
- At least three iterations
- Watch variable trend:
- Increasing?
- Decreasing?
- Unchanged?
- Check condition truth value after each iteration
- If condition never changes:
- Loop is infinite
Final Quality Checklist
- Loop condition identified
- Termination variable identified
- Update direction analysed
- Logical operators checked
- Boundary conditions verified
Final Lock-In Rules
- Infinite loops are logic errors, not syntax errors
- Termination variables must change
- Direction of change matters
- REPEAT UNTIL logic is reversed
- Correct detection = guaranteed reasoning marks
