Our program is already set up to handle two integer values. It declares two integer variables and then outputs their values.
Variables can be declared in basically two ways, with or without initialization:
type variable_name = initializer_expression;
You can list several variables (with or without initialization) after the data type, separating them with commas.
Our program declares and initializes two integer variables:
int count = 3, number = 5;So
type, each of
Note: Some programmers prefer to put both declarations on the same line:int count = 3, number = 5;
or to use separate declaration statements:int count = 3; int number = 5;
Question #2.1.1: What do you think this program will display on the screen when it executes?
Note: there is no wrong answer to this question. The intent of such questions is to build your skill at reading programs and to encourage you to predict how a program will execute. You'll "fix" wrong answers in later questions.
Compile and execute the program.
Question #2.1.2: Compare your answer to the previous question and the actual output. How do your answers differ (if at all)? Explain any differences.
The original program initializes
number to specific
values. Those same values should be printed out when you execute the
program. But maybe we just got lucky.
Change the initial values of
Question #2.1.3: Predict what this new version will display when it executes.
Compile and execute this new version.
Question #2.1.4: What did the program display? Was your prediction correct?
As noted above, initializing a variable is not required. Drop the initializations so that the declaration looks like this:
int count, number;
Question #2.1.5: Predict what this version will display when it executes.
Compile and execute this version.
Question #2.1.6: What did the program display? Was your prediction correct?
number were initialized to 0 by
default. Do not count on this. Some compilers will initialize
integer variables to 0, but not all of them. Depending on the
compiler and even the version of the compiler,
number may have been initialized to seemingly random "garbage" values.
Compilers change and/or you may change to a different compiler, so it's risky to rely on automatic initialization. Since initialization is so easy, it's much better for you to do it explicitly.
Change your program back so that
number are initialized to
int count = 66, number = -5;
You've been told that variables must be declared before you can use
them. What happens if you don't? Use
comment out the declarations of
(i.e., place the two slashes at the beginning of each line used to
Try compiling it. Oops!
Question #2.1.7: What is the first compiler error that you get?
Variable declarations are a common source of programming error. You may forget to declare a variable, you may misspell it, or you may put it in the wrong place. Learn to recognize this error message; when you see it, it is most likely caused by a problem with a variable declaration.
Now, restore the declarations (i.e., uncomment them).
What happens if you declare the same variable more than once? To find out, duplicate these declaration lines in your program and then recompile it.
Question #2.1.8: What is the first error message that your compiler gives you?
Now remove the declaration lines you added.
Some words in C++ have a special meaning, and you cannot use them in other contexts. Such a keyword, or reserved word, can be used only for its particular meaning, not for a variable that you create.
double is a reserved word. You cannot use it as
the name of a variable. Try it: replace
double in the declaration. Try compiling your program.
Question #2.1.9: What is the first compiler error that you get?
count in the declaration before you forget where
it should go.
So, we must avoid keywords. C++ books will usually have a list of all the C++ keywords, perhaps in an appendix. You should check the list occasionally so that you become familiar with the keywords.
So what does constitute a valid identifier?
An identifier must begin with a letter and can be followed by letters
Z), digits (
and underscores (
Replace the variable
count in your program with
Replace every use of the variable
Question #2.1.10: Predict: what will your program display?
Compile and execute your program. Your program will compile; make sure you've changed every count variable to value3.
Question #2.1.11: What did your program actually display? Was your prediction correct?
Replace the variable
value3 in your program with
Question #2.1.12: Predict: what will your program display?
Compile and execute your program.
Question #2.1.13: What did your program actually display? Was your prediction correct?
Replace the identifier
your program. (After all, if
value3 is fine, why not
Compile your program.
Question #2.1.14: What is the first error message your compiler gave you? What's wrong with the identifier?
We could play this game all day, but you get the idea. Restore
count in your program.
Now, just because an identifier is valid does not mean that it's a
good identifier. There are several things to consider in picking
names for identifiers. The first is readability. A good identifier
should indicate what object it holds. What value would you expect to
find in a variable named
b? It could be a book, a Boolean, a
bird, or just about anything in the 'b' section of your dictionary;
book would be a much better name (if, in fact, it stored a
book object). In general, it's best to use full words or even
phrases for your identifiers to better describe the objects they hold.
You must also consider the form of the identifier. Conventions vary, but they're usually similar to the one we'll use for variables:
These declaration experiments are not restricted to integer variables and values. All variables must be declared, regardless of their type, and may also be initialized.