Identifying and Classifying Errors

Part of: Creative Development

Errors Are Part of Programming Every programmer produces errors, and a key Creative Development skill is finding and fixing them. AP CSP groups program errors into three kinds: - Syntax errors : the code breaks the language's rules, so it will not run. Example: a missing colon or unmatched parenthesis. - Runtime errors : the program starts but crashes during execution, such as dividing by zero or converting a non-number to an int. - Logic errors : the program runs without crashing but produces the wrong result because the reasoning is flawed. Logic errors are the trickiest because nothing announces them; only testing reveals them. Spotting a Logic Error Consider code meant to compute a sum. A logic error hides inside the loop: This runs fine but prints 6 instead of 10. The fix is total += n. Because the program never crashes, only a known-answer test exposes the mistake. Reducing Errors Programmers reduce errors by validating input and handling edge cases. For example, counting how many values are positive must work even when some are zero or negative: Handling each value carefully, including zero, which is not positive, avoids logic errors. Key takeaways: - Errors are syntax, runt

Challenge: Count Positive Numbers