Algorithmic Problem-Solving Guides: Debugging Algorithms Using Logical Reasoning (Copy)
Debugging Algorithms Using Logical Reasoning
What Debugging Means In Exam Context
- Debugging is the process of:
- Finding logical errors
- Explaining why an algorithm fails
- Correcting the logic without rewriting everything
- In exams, debugging is tested through:
- “Identify the error”
- “Explain why the algorithm does not work”
- “Correct the algorithm”
- Trace-table based fault detection
Core Examiner Rule (Critical)
- Examiners care more about:
- Reasoning
- Explanation
than just the corrected code
- A correct fix without explanation can still lose marks
What Counts As A Logical Error
- Code runs
- Syntax looks fine
- Output is wrong
Examples:
- Wrong condition
- Wrong variable updated
- Loop stops too early or never stops
- Output placed incorrectly
The 6-Step Logical Debugging Method (Use This Always)
Step 1: Understand What The Algorithm Is SUPPOSED To Do
- Read the problem statement
- Write the expected behaviour in plain words
- Never debug blindly
Step 2: Identify Inputs And Expected Output
- Ask:
- What values go in?
- What result should come out?
- This gives you a reference point
Step 3: Trace The Algorithm Line By Line
- Use a trace table
- Track:
- Variable changes
- Loop conditions
- IF decisions
- Most logical errors reveal themselves here
Step 4: Compare Actual Behaviour With Intended Behaviour
- Ask:
- Where does behaviour first differ?
- The first wrong step is usually the real bug
Step 5: Identify The Error Category
- Loop error
- Condition error
- Initialisation error
- Update error
- Output placement error
Step 6: Fix The Logic With Minimal Change
- Do not rewrite everything
- Change only what causes the error
- Examiners prefer small, precise fixes
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 Logical Error Categories (With Debugging Strategy)
Category 1: Initialisation Errors
Faulty Code
total ← total + num
- Problem
totalwas never initialised
- Logical Reasoning
- Accumulators must start from a known value
- Fix
total ← 0
total ← total + num
Category 2: Loop Condition Errors
Faulty Code
WHILE count < 10 DO
count ← count + 1
ENDWHILE
- Intended
- Loop should run 10 times
- Actual
- Runs only 9 times
- Reason
- Starts from 1, stops at 9
- Fix
WHILE count <= 10 DO
Category 3: Infinite Loops
Faulty Code
WHILE num > 0 DO
OUTPUT num
ENDWHILE
- Problem
numnever changes
- Logical Reasoning
- Condition variable must be updated
- Fix
WHILE num > 0 DO
OUTPUT num
num ← num - 1
ENDWHILE
Category 4: Wrong Variable Updated
Faulty Code
FOR i ← 1 TO 5
total ← total + i
i ← i + 1
NEXT i
- Problem
- Loop variable updated manually
- Effect
- Skips values
- Fix
REMOVE i ← i + 1
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
Debugging IF / ELSE Logic Errors
Example 1: Condition Reversal
Faulty Code
IF mark < 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
- Logical Error
- Condition reversed
- Reasoning
- Pass should be ≥ 50
- Fix
IF mark >= 50 THEN
Example 2: Overlapping Conditions
Faulty Code
IF score >= 80 THEN
grade ← "A"
ELSEIF score >= 70 THEN
grade ← "B"
ELSEIF score >= 80 THEN
grade ← "C"
ENDIF
- Problem
- Third condition unreachable
- Reasoning
>= 80already handled
- Fix
ELSEIF score >= 60 THEN
Debugging Loop Placement Errors
Faulty Code
FOR i ← 1 TO 10
INPUT num
NEXT i
IF num > 5 THEN
count ← count + 1
ENDIF
- Intended
- Count how many inputs > 5
- Actual
- Only last input checked
- Logical Reasoning
- Condition must apply to every input
- Fix
FOR i ← 1 TO 10
INPUT num
IF num > 5 THEN
count ← count + 1
ENDIF
NEXT i
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
Debugging Array Logic Errors
Example: Wrong Index Used
Faulty Code
FOR i ← 1 TO 5
sum ← sum + arr[1]
NEXT i
- Problem
- Same element added repeatedly
- Reasoning
- Index should change with loop
- Fix
sum ← sum + arr[i]
Example: Out-Of-Range Access
Faulty Code
FOR i ← 1 TO n + 1
OUTPUT arr[i]
NEXT i
- Problem
arr[n+1]invalid
- Fix
FOR i ← 1 TO n
Debugging String Logic Errors
Example: Direct Modification Attempt
Faulty Code
text[3] ← "X"
- Logical Error
- Strings cannot be modified directly
- Correct Reasoning
- Must rebuild string
- Fix
newText ← ""
FOR i ← 1 TO LENGTH(text)
IF i = 3 THEN
newText ← newText & "X"
ELSE
newText ← newText & text[i]
ENDIF
NEXT i
text ← newText
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
How Examiners Want Debugging Answers Written
Examiner-Friendly Structure
- State the error
- Explain why it is wrong
- Show the corrected logic
Example Answer Style
- “The loop never terminates because the condition variable is not updated.”
- “This causes an infinite loop.”
- “Adding an update statement fixes the error.”
Common Debugging Mistakes In Exams
- Guessing output
- Ignoring initialisation
- Fixing symptoms, not cause
- Rewriting entire algorithm
- No explanation provided
Debugging Checklist (Memorise This)
- Are all variables initialised?
- Does every loop terminate?
- Are conditions correct and reachable?
- Are correct variables updated?
- Is output placed correctly?
Exam-Ready Debugging Template
Error:
<describe the logical error>
Reason:
<explain why it causes incorrect behaviour>
Correction:
<show corrected line(s)>
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
- Understand intent first
- Trace before fixing
- Fix the cause, not the result
- Explain your reasoning
- Small changes score big marks
