AP Computer Science PrinciplesÜnite 1 · Creative Development
Identifying and Correcting Errors
Recognize syntax, runtime, logic and overflow errors, and use testing, hand tracing and debugging strategies to find and fix them.
Types of errors
| Error | What happens | Example |
|---|---|---|
| syntax error | the rules of the language are broken; the program does not run | a missing bracket |
| runtime error | the program fails while running | dividing by zero, an index outside a list |
| logic error | the program runs but gives the wrong result | using > instead of ≥ |
| overflow error | a number is too large for the space reserved for it | adding past the largest value |
Logic errors are the hardest to find because nothing crashes.
Finding errors
- Test cases: choose inputs and the expected outputs, including boundary and unusual values (0, negative numbers, empty lists, the largest allowed value).
- Hand tracing: follow the code line by line, recording variable values in a table.
- Visualizations and debuggers: step through code.
- Adding output statements to display intermediate values.
Example of a logic error
The procedure should count the scores of at least 50:
count ← 0
FOR EACH s IN scores
{
IF (s > 50)
{
count ← count + 1
}
}
A score of exactly 50 is not counted. The condition should be s ≥ 50. A test case containing the boundary value 50 reveals the error.
Quick summary
- Syntax errors stop the program; logic errors give wrong answers silently.
- Test boundaries and unusual inputs.
- Hand-trace with a table of values.
Konu quizi
5 soru. Geçmek için en az %80 doğru gerekiyor; geçince sonraki konu açılır.
Quizi çözüp ilerlemeni kaydetmek için giriş yap. Hesap açmak ücretsiz.
Giriş yap ve quizi çöz