Section 4.6 Common Variable Errors
We all make mistakes. In coding there are several types of errors we are all prone to. Some are akin to spelling or grammar errors (we call them syntax errors) and some are of logical nature, for example. It can be helpful to think about potential errors before getting started so that you can be on the lookout. Let’s look at some common errors in working with variables.
The following three activities present erroneous code. Can you spot the error and fix it? Don’t be afraid to play around and try out multiple solutions. You cannot break anything! Remember that you can always go back to the original problem by clicking on the “Start fresh” button.
Subsection 4.6.1 Fix Me!
Activity 4.2.
You’d like to print your age to the screen. What is wrong with the following code? Can you fix it?
If you’d like our autograder to check your program for correctness, please do not change the printf()
statement.
admin.....open in new window
What was wrong with the given code?
Please paste your text submission into the box below.
Subsection 4.6.2 Fix me too...!
Activity 4.3.
Again, you’d like to print your age to the screen. And again, something is wrong with the following code. Can you fix it?
If you’d like our autograder to check your program for correctness, please do not change the printf()
statement.
admin.....open in new window
What was wrong with the given code?
Please paste your text submission into the box below.
Subsection 4.6.3 Fix me three...!
Activity 4.4.
Another error! You’d like to print your age to the screen. And again, something is wrong with the following code. Can you fix it?
If you’d like our autograder to check your program for correctness, please do not change the printf statement.
admin.....open in new window
What was wrong with the given code?
Please paste your text submission into the box below.
Subsection 4.6.4 Summary
Here is what we have learned:
Variable names can only contain letters, numbers and the underscore character “_”.
Variable names must start with a letter or an underscore “_”.
Variable names are case sensitive.
Variables need to be initialized (assigned a value) BEFORE they are used.