4A. Creating and Using Variables


Variables

A variable can hold one item of a given type. If you store a new item into a variable, it replaces the old item.

Creating a variable

Before you use a variable, you need to create, or declare, it. If T is a type and v is a name, then statement

  T v;
creates a new variable called v that can hold one item of type T. For example,
  int x;
creates a new variable called x of type int, and
  double frog;
creates a new variable called frog of type double. Notice the semicolon at the end. It is required.

You can create several variables of the same type together, as in

  double width, length, height;


Variable names

A variable name should begin with a letter. It can contain letters, digits and the underscore symbol (_), and can have any length. For example, gravity and x1 are allowed variable names.

A variable name cannot contain a space. To write a multi-word variable name, either capitalize the first letter of each word, as in ageOfReason, or use underscores, as in age_of_reason.

But whatever choice you make, be consistent. Variable ageOfReason must be written exactly like that each time you write it. Names longTrip and long_trip are different. The case of letters matters. Variable names N and n are not the same.



Assignment statements

Statements

A statement is a kind of command. It tells the computer to do something.

Assignment statements

Changing the value of a variable is called assigning a value to the variable. Statement v = E; (an assignment statement) computes the value of expression E then stores that value into variable v. For example,

  x = y + 1;
stores the value of y + 1 into x. The semicolon is required.


Sequences of statements

If you write several statements in a row, they are done one after the other. For example,

  w = 40;
  v = w + 1;
makes w = 40 and v = 41.


Distinguishing declarations from assignments

Notice that, when you declare a variable, the statement starts with the name of a type. But when you assign a value to a previously declared variable, the statement does not begin with the name of a type. Do not add a type to an assignment statement. Statement
  double z;
creates a new variable called z. Statement
  z = 1.0;
stores a value into a prevously created variable.

Right-to-left order

Be sure to notice that assignments are done from right to left. First, the expression on the right-hand side of = is computed, then the variable is changed. For example

  int x;
  x = 0;
  x = x + 1;
starts by creating variable x. Then it stores 0 into x. The third line starts by computing x+1, which is 1 since x is currently 0. Then it stores 1 into x.


Variables are not changed automatically

Assignment uses current values of variables. For example,

  y = 0;
  x = y;
  y = 1;
ends with x = 0 and y = 1. Changing the value of y in the third statement has no effect on x, since the second statement used the current value of y.



Variable initialization

Initialization

Statement

  int x;
does not store any value into x; x is uninitialized. There will be some value of type int in x, but you have no way of knowing what that value will be. It is junk.

The first time you store a value into a variable, you are said to be initializing the variable. Always be sure to initialize a variable before you use it.


Using uninitialized variables

C++ differs from Java in its handling of uninitialized variables. Java insists that the program must store a value into a variable before the variable is used. C++ has no such requirement. If you use a variable that has not been initialized, you get whatever junk value is in that variable.

There is no way for a program to ask whether a variable is uninitialized.


Creating initialized variables

You can declare and initialize a variable in single statement. Statement

  int r = 1;
is equivalent to the two statements
  int r;
  r = 1;

Creating and initializing several variables of the same type is easy. Statement

  int w = 0, n = 1;
creates two new variables, w and n.

But be sure to remember that a declaration, which begins with the name of a type, creates a new variable. Consider the following two lines.

  int n = 1;
  int n = n + 1;
The first line creates variable n and initializes it to be 1. The second line tries to create another variable called n. That is not allowed. You will get a compile error. To change the value of previously created variable n, omit the type, as in
  int n = 1;
  n = n + 1;

(Of course, you would never write exactly those two lines, since

  int n = 2;
is more direct. Sometimes I will write silly things in order to make a point.)



Summary

A variable holds one value. You must create a variable before you use it. Statement

  int q;
creates a variable called q that holds on value of type int. The initial value of q is unspecified. It could be anything. Statement
  int q = 0;
creates variable q and initializes it to hold 0.

You can create several variables of the same type in one statement. For example,

  int a,b,c;
is equivalent to
  int a;
  int b;
  int c;

An assignment statement changes the value of a variable. Statement

  a = 2*a + b;
first computes the value of expression 2*a + b, then stores that value into previously created variable a.


Exercises

  1. Is there a default value to which variables of type int are initially set? Answer

  2. Rewrite the following so that it only computes max(x,y) once.

      int x = max(x,y)*max(x,y) + 1;
    
    Answer

  3. Is the following sequence of statements allowed?

      int d = 40;
      int d = d + 1;
    
    Answer

  4. Is the cat an acceptable name for a variable? Answer

  5. If you use a variable that you have not created, will the compiler create it automatically for you? Answer

  6. What is the value of variable x after doing the following statement?

      double x = 1/3;
    
    Answer