Lab 2: Experiment 1


The Basic Program

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;
   type variable_name;

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 int is the type, each of count and number is a variable_name, and 3 and 5 are initializer_expressions.
 
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;

  Following the declarations is a statement to display the values. You must use such 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. 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.

Initial Values in a Declaration

The original program initializes count and 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 count and number to 66 and -5, respectively.

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?

No Initial Values

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?

Most likely, count and 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, count and 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 count and number are initialized to 66 and -5 again:

   int count = 66,
       number = -5;

No Declaration at All

You've been told that variables must be declared before you can use them. What happens if you don't? Use // to comment out the declarations of count and number (i.e., place the two slashes at the beginning of each line used to declare count and number.

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).

Declaring Twice

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.

Identifiers for Variables

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.

For example, double is a reserved word. You cannot use it as the name of a variable. Try it: replace count with double in the declaration. Try compiling your program.

Question #2.1.9: What is the first compiler error that you get?

Restore 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 (a through z and A through Z), digits (0 through 9), and underscores (_).

Replace the variable count in your program with value3. Replace every use of the variable count.

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 integerVariable.

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 integerVariable with 3value in your program. (After all, if value3 is fine, why not 3value?) 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 the variable 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:

Not Just Integers

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.


Back to the Lab Exercise  |  Forward to the Next Experiment


Report errors to Larry Nyhoff (nyhl@cs.calvin.edu)