AP Computer Science Principles · Unit 1: Creative Development
1.4 Identifying and Correcting Errors
Types of Errors
Syntax errors violate the rules of the programming language (like a typo) and prevent the program from compiling or running. Logic errors occur when a program runs without crashing but produces incorrect or unexpected results because the algorithm's design is flawed. Both types must be found and fixed during development.
Debugging Strategies
Debugging is the process of finding and fixing errors. Effective strategies include testing with a variety of inputs, checking edge cases (unusual or extreme inputs), using print/trace statements to see intermediate values, and reading error messages carefully. Breaking a program into smaller pieces and testing each part separately makes bugs easier to isolate.
Testing and Incremental Development
Incremental development means writing and testing small chunks of code before adding more, so errors are caught early and are easier to trace. Using test cases with known expected outputs helps verify correctness. Collaboration, like having someone else read your code, can also reveal errors you overlooked.
Key terms
- Syntax error
- A violation of the programming language's rules that prevents the program from running correctly.
- Logic error
- An error where the program runs but produces incorrect results due to a flawed algorithm.
- Debugging
- The process of identifying and fixing errors in a program.
- Edge case
- An unusual or extreme input used to test the limits of a program.
- Incremental development
- Writing and testing small pieces of a program before adding more, to catch errors early.
Try an AP-style question
Code
sum ← 0
FOR EACH n IN [2, 4, 6]
{
sum ← n
}
DISPLAY(sum)The goal is to display the total of the list. What is displayed, and what kind of error is this?
- 6; a logic error
- 2; a syntax error
- 12; no error
- 0; a runtime error
Show the answer
A. sum ← n should be sum ← sum + n.
Practice 8 AP-style questions with instant explanations, flashcards and spaced review.
Study 1.4 free in APrep