Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings Prev Up Next
\(\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 4.7 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.7.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?
Record your answer below.
Subsection 4.7.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?
Record your answer below.
Subsection 4.7.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?
Record your answer below.
Subsection 4.7.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.