Our program is already set up to handle two integer values. It declares two integer variables and then prints their values.
Variables can be declared in basically two ways, with or without initialization:
You can list several variables (with or without initialization) after the data type, separating them with commas.
Our program has opted to declare and initialize two integer variables:
int i = 3, j = 5;So
After the declaration is a statement to print the values. You must use explicit output statements to see the values in your variables.
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, or any question that asks you to speculate what your program will do. The intent of these questions is to build your skill at reading programs and encouraging you to speculate about 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? Explain why they different.
The original program initializes
j 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
j to be
Question #2.1.3: What does the declaration line look like now?
Question #2.1.4: What will this new version print when it executes?
Compile and execute this new version.
Question #2.1.5: What did the program display? Evaluate how good your prediction was.
As noted above, initializing a variable is not required. Drop the initializations so that the declaration looks like this:
int i, j;
Question #2.1.6: Predict what this version will print when it executes.
Compile and execute this version.
Question #2.1.7: What did the program display? Evaluate how good your prediction was.
j 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,
j may have been initialized to seemingly random values.
Compilers change, and you may switch compilers, so it's risky relying on automatic initialization. Since initialization is so easy, it's much better for you to do it explicitly.
Change your program back so that
You've been told that variables must be declared before you can use
them. What happens if you don't? Use a
// comment to
comment out the declaration of
j (i.e., place
the two slashes before
int, on the same line as the
Try compiling it. Ooops!
Question #2.1.8: What is the first compiler error that you get?
Variable declarations are a very 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 the error message that you just got; when you see it, you most likely have a problem with a variable declaration.
Restore the declaration (i.e., uncomment it).
What happens if you declare the same variable more than once? Copy the declaration line in your program two times. Compile your program.
Question #2.1.9: What is the first error message that your compiler gives you?
Better not do that. Remove one of the declarations.
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.
int is a reserved word. You cannot use it as
the name of a variable. Try it: replace the variable
int in the declaration. Try compiling your program.
Question #2.1.10: What is the first compiler error that you get?
i to your program before you forget where it should
So, we must avoid keywords. Any C++ book will list the C++ keywords; check the list occasionally so that you become familiar with the keywords. It's probably not worthwhile deliberately memorizing them; you'll end up learning them through experience.
So then what constitutes a valid identifier? An identifier must begin with a
letter and can be followed by letters (
Z), digits (
9), and underscores (
Replace the variable
i in your program with
Replace every use of the variable
Question #2.1.11: Predict: what will your program display?
Compile and execute your program. Your program will compile; make sure you've changed every i variable to i3.
Question #2.1.12: What did your program actually display? Compare the output to your prediction.
Replace the variable
i3 in your program with
integerVariable. (Using your editor's search-and-replace makes this
faster than doing it by hand!)
Question #2.1.13: Predict: what will your program display?
Compile and execute your program.
Question #2.1.14: What did your program actually display? Compare the output to your prediction.
Replace the identifier
your program. (Hey, if
i3 is fine, why not
Compile your program.
Question #2.1.15: 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
i 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---open up your dictionary to the 'b's.
book would be a much better name (if, in fact, it stored a
Some names have implied meanings. If your value is an integer used
to count, the identifiers
very popular. The identifiers
are often used for real numbers. If you use these in other contexts,
you may confuse other programmers. 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 particular to integer variables or integer values. All variables must be declared regardless of their type, and they should all be initialized also regardless of their type. The types and initializer expressions change, but never go away.