Welcome to the Debugging Suite!

In the world of Big Idea 1: Creative Development, writing code is only half the battle. The other half? Finding out why it didn't work the first time! If you have ever felt frustrated because your code didn't do what you expected, don't worry—you are actually doing real computer science. Professional programmers spend a huge portion of their time identifying and correcting errors. Think of this chapter as your "Detective Training" for code.

1. Meet the Culprits: Types of Errors

Before we can fix a problem, we need to know what kind of problem we are looking for. In AP Computer Science Principles, we categorize errors into four main types:

A. Syntax Errors

Imagine trying to read a sentence like: "The cat the sat mat on." It doesn't make sense because the rules of grammar were broken. A syntax error is a "grammar" mistake in your code. You might have forgotten a bracket, misspelled a command, or used the wrong punctuation.
Note: Since the AP Exam uses pseudocode, you won't usually have to find tiny syntax errors on the test, but you must know what they are!

B. Runtime Errors

A runtime error happens after you start the program. Everything looks fine to the computer at first, but while the program is running, it hits a "dead end" and crashes.
Common Example: Trying to divide a number by zero \( (x / 0) \) or trying to access the 10th item in a list that only has 5 items. The program simply stops and says, "I can't do this!"

C. Logic Errors

These are the sneakiest errors. A logic error occurs when the program runs perfectly without crashing, but it produces the wrong output.
Example: You want to calculate the area of a square, but you accidentally write \( Area = side + side \) instead of \( Area = side * side \). The computer does exactly what you told it to do, but your "logic" was flawed.

D. Overflow Errors

Computers have a limited amount of "physical space" (memory) to store numbers. An overflow error happens when a program tries to store a number that is too large for the computer to handle. It’s like trying to fit 10 gallons of water into a 5-gallon bucket—it just won't fit!

Quick Takeaway: Syntax errors stop the code from starting; Runtime errors crash it while it's running; Logic errors give the wrong answer; Overflow errors happen when numbers are too big.

2. The Detective's Toolbox: How to Find Errors

Once you know there is a bug, how do you find it? Here are the official strategies you need to know:

Hand Tracing

This is exactly what it sounds like: you act like the computer! You take a piece of paper and a pencil, look at your code, and write down the value of every variable as it changes line-by-line.
Pro-Tip: Draw a small table where each column is a variable name. As you "execute" each line of code, cross out the old value and write the new one. This is the best way to catch logic errors.

Visual Displays of Data

Sometimes, looking at a wall of numbers is confusing. By using graphs, charts, or other visual displays, you can often spot patterns or "outliers" (numbers that don't belong) that point you toward an error in your algorithm.

Extra Output Statements

If you aren't sure where things are going wrong, you can add temporary output statements (like \( DISPLAY(x) \)) inside your code. By "peeking" at the values of variables while the program is running, you can see exactly where the math starts to fail.
Remember: Once you fix the bug, don't forget to remove these extra "print" statements!

Test Cases

A test case is a set of inputs designed to check if the program works. To be effective, you should test:

  • Expected inputs: Does it work with normal numbers?
  • Edge cases: Does it work with the smallest possible value? The largest? Zero?
  • Invalid inputs: What happens if a user types a word when you asked for a number?

3. Correcting the Errors

Identifying the error is 90% of the work. Once found, correcting it involves:

  1. Re-evaluating the requirements: Did you understand the goal of the program?
  2. Modifying the code: Changing the symbols or statements.
  3. Retesting: Just because you fixed one bug doesn't mean you didn't accidentally create another! Always run your test cases again.

4. Common Mistakes to Avoid

The "Off-by-One" Error: This is a classic logic error. On the AP Exam, remember that list indices start at 1. If you have a list of length \( n \), trying to access index \( 0 \) or index \( n + 1 \) will result in an error.

Mixing up AND/OR: Using \( condition1 \textbf{ AND } condition2 \) when you meant \( \textbf{ OR } \) is a very common logic error that leads to the wrong code path being taken.

Did you know? The term "bug" became famous when Grace Hopper, a pioneer in computer science, found a literal moth stuck inside a computer relay in 1947, which was causing the machine to fail. She taped the moth into her logbook and called it "debugging"!

Key Takeaway for the Exam: Be ready to look at a code segment and a set of inputs to determine why the output is wrong. Usually, you will be asked to either identify the line that is causing the logic error or choose the corrected version of the code.