Overview
All programmers make mistakes. The key skill is learning how to find, understand, and fix those mistakes. In AP CSP, you practice debugging — a systematic process of identifying and correcting errors so a program behaves as intended.
Debugging: finding, understanding, and fixing mistakes so programs behave as intended.
All programmers make mistakes. The key skill is learning how to find, understand, and fix those mistakes. In AP CSP, you practice debugging — a systematic process of identifying and correcting errors so a program behaves as intended.
| Type | When it Happens | Example | How to Fix It |
|---|---|---|---|
| Syntax Error | When the code violates language rules. | Missing parenthesis or misspelled keyword. | Read error messages; fix punctuation or spelling. |
| Logic Error | The program runs but gives wrong results. | Using + instead of - in a formula. |
Trace with prints; compare expected vs. actual behavior. |
| Runtime Error | The program crashes while running. | Divide by zero; index out of range; null reference. | Add checks/guards; handle exceptional conditions. |
Tip: Syntax errors usually stop code from running. Logic and runtime errors often allow it to run incorrectly.
A good program doesn’t just work — it fails safely with helpful messages.
Goal: Divide two numbers without crashing.
Original Pseudocode (with error):
num1 ← INPUT
num2 ← INPUT
result ← num1 / num2
DISPLAY result
Problem: When num2 = 0, program crashes (runtime error).
Fixed Version:
num1 ← INPUT
num2 ← INPUT
IF num2 ≠ 0 THEN
result ← num1 / num2
DISPLAY result
ELSE
DISPLAY "Error: Cannot divide by zero."
END IF
| Skill | Purpose |
|---|---|
| Identify errors | Recognize syntax, logic, and runtime problems. |
| Debug systematically | Follow a repeatable process to locate issues. |
| Test programs | Use varied inputs and edge cases to validate correctness. |
| Correct & refine | Modify and verify code until results match expectations. |