Helping Kids with Coding For Dummies
Book image
Explore Book Buy On Amazon
It’s easy for kids to make semantic errors when they’re first learning to code. Unlike syntax errors, semantic errors are often more difficult to capture. This is because semantic errors are typically errors in the programming logic, rather than something that you typed incorrectly. Here, you find a couple of examples of semantic errors that you and your young coder might encounter in a few different programming languages.

Infinite loops

Infinite loops are loops that never end! They go on infinitely. This can be a problem because it might seem like the code just isn't working, but really the program is just running forever and ever.

Using Java

If you wrote a small Java program where you wanted to print the numbers 0 through 9, you might write something like this:
for(int index = 0; index < 10; index--)
{
 System.out.println(index);
}
But there is an error in this code! Instead of updating the index to be index + 1, the code updates the index to be index – 1! So the code does the following:
index = 0
Is index < 10? Yes
Print index 0
index = index – 1 index = -1
Is index < 10? Yes
Print index -1
index = index – 1 index = -2
Is index < 10? Yes
Print index -2
index = index – 1 index = -3
Is index < 10? Yes
Print index -3
This continues forever, because it’s impossible for index to be greater than or equal to 10. So when you run the Java code, the program continues to print forever, until you kill the program!

Using Scratch

Although infinite loops can be a problem, some programming languages deliberately have implemented infinite loops to make some pretty neat effects! For example, in Scratch there is a forever block which can do some cool things.

Off by one

Another very common error to run into is called an off by one error. This is very common when dealing with lists and iterating through lists.

Using Scratch

Scratch, as usual, handles off by one errors for the user without really indicating there’s a problem. For example, the image below shows a program that loops through a list of pets and has the sprite say Hi petName, where petName is replaced with the item from the list (either Luke, Winston, or Princess). The loop repeats four times, but there are only three items in the list. Instead of completely breaking, on its last iteration, Scratch prints Hi with nothing after it.

semantic errors coding An example of a Scratch program with an off by one error.

Using Python

Other programming languages are not as forgiving. For example, in Python you might have the following program to say hello to the three pets:
pets = ['Luke’, 'Winston', 'Princess']
for x in range(1, 3):
print (‘Hi ‘ + pets[x])
If you run this program, the output would be:

The reason ‘Hi Luke’ doesn’t print is because lists in Python start at 0, not at 1. The correct code would be:

pets = ['Luke', 'Winston', 'Princess']
for x in range(0, 3):
print ('Hi ' + pets[x])
The range function used in Python:
range(0, 3)
Represents the elements 0, 1, and 2 because the range function includes the first number but excludes the second.

Another version of an off by one error in Python would be if you went beyond the length of the list. For example:

pets = ['Luke', 'Winston', 'Princess']
for x in range(0, 4):
print ('Hi ' + pets[x])
This causes even more of an issue, because instead of simply missing an element in the list, you’re trying to access an element that never existed in the first place. The output for running this code is:
Hi Luke
Hi Winston
Hi Princess
Traceback (most recent call last):
 File "filename.py", line 4, in 
  print ('Hi ') + pets[x]
IndexError: list index out of range
There is an actual error, because the data for pets[4] doesn’t exist, so the computer cannot resolve it; therefore it doesn’t know what to do.

Off by one errors can be really tricky for young coders, especially if they’re switching between languages where lists start at 1 versus lists that start at 0.

About This Article

This article is from the book:

About the book authors:

Camille McCue, PhD, is Director of Curriculum Innovations at the Adelson Educational Campus in Las Vegas where she leads the Startup Incubator, teaches STEM, and kickstarts K-12 learning initiatives. Sarah Guthals, PhD, co-founded an ed-tech company and now continues to build technology for kids to learn, create, and share safely online. She loves to teach teachers how to teach coding in the classroom.

This article can be found in the category: