Algorithmic Problem-Solving Guides: Identifying And Fixing Logical Errors (Copy)
Identifying And Fixing Logical Errors
What A Logical Error Is (Exam Definition)
- A logical error occurs when:
- The algorithm runs
- Syntax is correct
- But the output is wrong
- These are the most tested and most misunderstood errors in exams
Core Examiner Rule (Extremely Important)
- Logical errors are identified by:
- Reasoning
- Tracing
- Understanding intent
- Not by guessing output
- Not by rewriting the whole algorithm
The Difference Between Error Types (Must Be Clear)
| Error Type | What Happens |
|---|---|
| Syntax error | Code does not run |
| Runtime error | Program crashes |
| Logical error | Program runs but gives wrong result |
The 5-Step Method To Identify Logical Errors
Step 1: Understand The Intended Outcome
- Read the question
- Write in plain words:
- What should this algorithm do?
- This is your reference point
Step 2: Identify Inputs And Expected Outputs
- Ask:
- What values go in?
- What should come out?
- Logical errors always appear when:
- Actual output ≠ expected output
Step 3: Trace The Algorithm Step By Step
- Use:
- Trace tables
- Dry runs
- Track:
- Variable values
- Condition results
- Loop iterations
Step 4: Locate The First Incorrect Behaviour
- The first wrong step matters most
- Everything after that is a consequence
Step 5: Apply A Minimal Fix
- Fix only the faulty logic
- Do not redesign the algorithm
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
Category 1: Initialisation Errors
Faulty Code
total ← total + value
Logical Problem
totalhas no starting value- Accumulator logic broken
Reasoning
- Accumulators must start from a known base value
Fix
total ← 0
total ← total + value
Category 2: Loop Boundary Errors
Faulty Code
FOR i ← 1 TO 9
INPUT num
NEXT i
Intended
- Input 10 numbers
Logical Error
- Loop runs only 9 times
Fix
FOR i ← 1 TO 10
Faulty Code (Off-By-One)
WHILE count < 10 DO
count ← count + 1
ENDWHILE
Logical Issue
- Loop stops too early
Fix
WHILE count <= 10 DO
Category 3: Infinite Loop Errors
Faulty Code
WHILE number > 0 DO
OUTPUT number
ENDWHILE
Logical Error
- Condition variable never changes
Reasoning
- Loop condition stays TRUE forever
Fix
WHILE number > 0 DO
OUTPUT number
number ← number - 1
ENDWHILE
Category 4: Wrong Variable Updated
Faulty Code
FOR i ← 1 TO 5
sum ← sum + i
i ← i + 1
NEXT i
Logical Error
- Loop variable manually modified
Effect
- Skipped iterations
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
Category 5: Condition Logic Errors
Condition Reversal
Faulty Code
IF mark < 50 THEN
OUTPUT "Pass"
ENDIF
Logical Error
- Pass condition reversed
Fix
IF mark >= 50 THEN
Overlapping Conditions
Faulty Code
IF score >= 80 THEN
grade ← "A"
ELSEIF score >= 70 THEN
grade ← "B"
ELSEIF score >= 80 THEN
grade ← "C"
ENDIF
Logical Error
- Third condition unreachable
Fix
ELSEIF score >= 60 THEN
Category 6: IF Placement Errors
Faulty Code
FOR i ← 1 TO 10
INPUT num
NEXT i
IF num > 5 THEN
count ← count + 1
ENDIF
Logical Error
- Condition applied to only last input
Fix
FOR i ← 1 TO 10
INPUT num
IF num > 5 THEN
count ← count + 1
ENDIF
NEXT i
Category 7: Array Logic Errors
Wrong Index Used
Faulty Code
sum ← sum + arr[1]
Logical Error
- Same element reused
Fix
sum ← sum + arr[i]
Out-Of-Range Access
Faulty Code
FOR i ← 1 TO n + 1
OUTPUT arr[i]
NEXT i
Logical Error
- Accesses invalid index
Fix
FOR i ← 1 TO n
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
Category 8: String Logic Errors
Direct Character Modification
Faulty Code
text[2] ← "X"
Logical Error
- Strings cannot be modified directly
Fix
newText ← ""
FOR i ← 1 TO LENGTH(text)
IF i = 2 THEN
newText ← newText & "X"
ELSE
newText ← newText & text[i]
ENDIF
NEXT i
text ← newText
Category 9: Output Placement Errors
Faulty Code
FOR i ← 1 TO 5
total ← total + i
OUTPUT total
NEXT i
Logical Error
- Outputs partial results instead of final
Fix
FOR i ← 1 TO 5
total ← total + i
NEXT i
OUTPUT total
How Examiners Want Fixes Explained
Examiner-Approved Answer Style
- Identify the error
- Explain why it is incorrect
- Show corrected logic
Example Explanation
- “The loop never ends because the condition variable is not updated.”
- “This causes an infinite loop.”
- “Updating the variable inside the loop fixes the error.”
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 Error Identification Checklist
- Are all variables initialised?
- Do all loops terminate?
- Are conditions correct and reachable?
- Are correct variables updated?
- Is output placed after processing?
Exam-Ready Error-Fixing Template
Error:
<describe the logical error>
Reason:
<explain why it causes incorrect behaviour>
Correction:
<show corrected line(s)>
One-Line Rules To Memorise
- Code can run and still be wrong
- Trace before fixing
- Fix the first wrong step
- Minimal changes score more
- Explanation matters as much as correction
