4A. 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;



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


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.

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

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 it. 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:

  int w = 0, n = 1;



Abbreviations for assignments

Increment and decrement

It is common to find that you want to change a variable by adding or subtracting 1. Of course,

  n = n + 1;
will change n to have a value one larger than its former value. But there are some convenient abbreviations. Statement
  n++;
means the same thing as n = n + 1; and statement
  n--;
is equivalent to statement
  n = n - 1;


Operator assignment

Statement

  x += E;
is equivalent to
  x = x + (E);
For example,
  kangaroo += 2;
is equivalent to
  kangaroo = kangaroo + 2;

You can use any binary operator (+, −, *, etc.) with =. For example, statement

  x *= n + 1;
has the same effect as statement
  x = x * (n+1);



Exercises

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

  2. Rewrite the following so that it does not compute max(x,y) twice.

      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