01 / 21
!

Identifying & Correcting
Errors

Learn to find, classify, and fix bugs in programs: from syntax and logic errors to runtime exceptions and beyond.

AP Computer Science Principles

Why Errors Matter

Every program, no matter how carefully written, can contain errors (also called bugs). Identifying and correcting errors is one of the most important skills a programmer develops. In the real world, a single undetected bug can crash an app, corrupt data, or produce wrong results.

The Three Types of Errors

Syntax Errors
Violations of the language's grammar rules. The program cannot run at all.
Runtime Errors
The program starts but crashes mid-execution: division by zero, accessing invalid data, etc.
Logic Errors
The program runs and finishes, but produces incorrect results. The hardest to find.

The Error Lifecycle

Write Code Test / Run Find Error Fix Error Repeat until correct

Debugging is an iterative process: you write code, test it, find errors, fix them, then test again. Professional developers spend roughly 50% of their time debugging.

Syntax Errors Explained

A syntax error occurs when your code violates the grammar rules of the programming language. The interpreter or compiler catches these before execution and refuses to run the program.

1 # Missing colon at end of if-statement 2 IF (score > 90) ← Syntax Error! 3 DISPLAY("Great job!")

Runtime Errors Explained

A runtime error occurs while the program is running. The syntax is valid, but an illegal operation is attempted during execution.

1 x ← 10 2 y ← 0 3 result ← x / y ← ERROR: division by zero!
Division by Zero
Attempting to divide a number by \(0\).
Index Out of Range
Accessing a list position that doesn't exist.

Logic Errors Explained

A logic error produces wrong output without crashing. The program runs to completion but doesn't do what you intended. These are the hardest to detect because there is no error message.

1 # Goal: compute average of three scores 2 a ← 80 3 b ← 90 4 c ← 70 5 avg ← a + b + c / 3 ← Logic error! 6 # Evaluates as: 80 + 90 + (70/3) = 193.3 7 # Intended: (80 + 90 + 70)/3 = 80

Key Distinction: Error Types

Syntax Runtime Logic
When caught Before running During execution After testing output
Program runs? No Partially Yes
Error message? Yes Yes No
Difficulty to fix Easy Medium Hard
Example Missing paren Divide by \(0\) Wrong formula

How to Debug: Step by Step

1
Read the error message. It usually tells you the line number and error type (for syntax and runtime errors).
2
Add DISPLAY statements. Print variable values at key points to trace execution flow.
3
Hand-trace the code. Walk through each line on paper, tracking variable values step by step.
4
Test with edge cases. Try inputs like \(0\), negative numbers, empty lists, and boundary values.
5
Fix one error at a time. Change one thing, re-test, then move to the next bug.

Hand-Tracing a Program

Hand-tracing means executing each line mentally (or on paper) and recording how variable values change. This is the single most powerful technique for finding logic errors.

1 x ← 1 2 REPEAT 3 TIMES 3 { 4 x ← x * 2 5 } 6 DISPLAY(x)
Iteration x before x after
112
224
348

Example 1: Finding a Syntax Error

The following program is supposed to check if a number is positive and display a message. Find and fix the error.

1 num ← 5 2 IF (num > 0 3 { 4 DISPLAY("Positive") 5 }
!
Line 2 is missing a closing parenthesis. The condition should be \((\text{num} > 0)\) with both parentheses.

Example 2: Fixing a Runtime Error

This program calculates the average of scores in a list, but crashes for some inputs.

1 scores ← [85, 92, 78] 2 total ← 0 3 FOR EACH s IN scores 4 { 5 total ← total + s 6 } 7 avg ← total / LENGTH(scores)
7 IF (LENGTH(scores) > 0) 8 { 9 avg ← total / LENGTH(scores) 10 }

Example 3: Tracing a Logic Error

This procedure should find the maximum value in a list, but it returns the wrong answer. Can you spot the bug?

1 PROCEDURE findMax(lst) 2 { 3 max ← 0 ← Bug is here! 4 FOR EACH item IN lst 5 { 6 IF (item > max) 7 { 8 max ← item 9 } 10 } 11 RETURN max 12 }

The Infamous Off-by-One Error

An off-by-one error is a special type of logic error where a loop iterates one too many or one too few times, or an index is shifted by one position.

1 # Goal: display items at positions 1 through 5 2 i ← 1 3 REPEAT UNTIL (i > 5) ← loops for i = 1,2,3,4,5 ✓ 4 { 5 DISPLAY(lst[i]) 6 i ← i + 1 7 } 8 9 # Common mistake: 10 REPEAT UNTIL (i >= 5) ← loops for i = 1,2,3,4 only ✕

Common Mistakes to Avoid

✕ Not Testing Edge Cases
Forgetting to test with empty inputs, \(0\), negative numbers, or single-element lists.
✕ Changing Multiple Things
Editing several lines at once. If it still fails, you won't know which change caused a new bug.
✕ Confusing ← and =
Using assignment (←) where you need a comparison (=), or vice versa, leading to unexpected behavior.

Debugging Strategy Summary

Strategy Best For How It Works
Read error messages Syntax, Runtime Locate line and error type
Add DISPLAY Logic, Runtime Print values to trace flow
Hand-tracing Logic Step through on paper
Test edge cases All types Try unusual inputs
Rubber duck Logic Explain code aloud line by line

Question 1 of 5

A student writes a program that runs but always outputs \(0\) instead of the correct sum. What type of error is this?

Question 2 of 5

What happens when this code executes?

Question 3 of 5

A student calls \(\text{countOccurrences}([3, 7, 3, 3], 3)\) and gets \(3\). Is this correct, and what type of error (if any) exists?

Question 4 of 5

What error does this code contain?

Question 5 of 5

Which debugging strategy is most effective for finding a logic error when the program produces no error message?