<< Hide Menu
7 min read•june 18, 2024
Minna Chow
Minna Chow
Errors! Gotta love them. Errors happen all the time when we're programming. The AP test will ask you to identify errors and you'll have to face them when making your Create task. Coding errors are also known as bugs, and they happen to everyone who's ever typed a line of code in their lives. Debugging is part of the coding process, so don't get discouraged if you come across some in your code.
In this guide, we'll be discussing common errors, how to detect errors and how to fix them. We'll also be looking at how this topic will be tested on the AP Exam.
Here's a list of common errors to get you started on your bug-hunting.
Syntax Errors: A syntax error occurs when the spelling and/or punctuation rules of the programming language aren't followed. For example, forgetting to close a set of parentheses or spelling a variable wrong, could cause your entire program to crash. A syntax error could also be failing to indent properly. This dependence on perfectly correct syntax can be one of the more frustrating things about coding.
Example: This code editor (Microsoft's Visual Studio Code) alerts me when I forget to close a parenthesis by making the unmatched parenthesis red.
Logic Errors: A mistake in a program's base logic that causes unexpected behavior. Besides syntax errors or errors caused by the limits of the device you're running your code on, this category covers most other errors.For example, let's say you have a dividing program, that takes two numbers (x and y) and divides x by y. Everything goes fine until you try dividing 5 by 0, after which the program crashes.
Run-time errors can differ by programming language. Different programming languages have different rules and syntax, and therefore they may handle errors in different ways. Overflow Errors: An error that occurs when a computer tries to handle a number that's outside of its defined range of values. Usually, this means that your computing device is trying to handle a number too big for it to process.
For more information, check out this explanation of Overflow Errors. If you're working with a coding language and an error crashes your program, there will often be an error message displayed. In the example for a syntax error above, the error message started with SyntaxError, making it clear what the error was.
However, error messages are not always so helpful. It's important to have many different methods to find and fix errors. Furthermore, bugs are tricky things, and it can be hard for even professional programmers to find and catch them all. (This is why a lot of software updates contain bug fixes. People are constantly finding and destroying bugs in their programs.)
Here are some challenges you might face as you're testing your code:
Good testing procedures include testing with many different test cases.
Question courtesy of the AP CSP CED, updated for 2021.
The answer is C: Moving the statement in line 5 so that it appears between lines 2 and 3.
First, let's take a look at the program's purpose: returning the number of times the value val appears in a list. For example, if val equaled 5 and 5 showed up twice in the list imputed, the program would return 2.
** If you're having trouble on a code question, try rewriting the code to the side. This can help you see the code lines in a different light. Just make sure to pay attention to details such as spacing when you rewrite.**
Rewritten without the brackets, the code looks like this:
PROCEDURE countNumOccurences(myList, val)
FOR EACH item IN myList
count ← 0
IF(item = val)
count ← count + 1
RETURN(count)
Let's make a test list [5, 2, 3, 5] and set val equal to 5 to see what the error might be. The program should return a value of 2.
Starting from the beginning, we set the count to 0. The first number in the list equals the value of val, so count now equals 1.
Because we're in a loop (which we'll talk more about in Big Idea 3), you're going to go back through the loop for the second item in the list, which is 2. We set count to zero again, and...
Wait, that isn't supposed to happen! The error in this code is that every time the loop resets, count also resets to zero.
How would we fix that? By taking count ← 0 outside of the loop. We need it to be before the loop starts, so we have the count variable to work with, but it can't be inside the loop itself.
The loop begins in line 3, so putting count ← 0 between lines 2 and 3 works perfectly.
That's a lot to take in, isn't it? Props to you for making it this far!
In our next Big Idea Guide, we'll cover data: how computers use and process it!
© 2024 Fiveable Inc. All rights reserved.