Skip to main content

Section 3.7 Multiple assignment

It is perfectly legal C++ to make more than one assignment to the same variable. The effect of the second assignment is to replace the old value of the variable with a new value. (This is why they are called β€œvariables”—they can vary!)
Listing 3.7.1. This active code reassigns fred from 5 to 7 and prints both values out.
The output of this program is 57, because the first time we print fred his value is 5, and the second time his value is 7.
Listing 3.7.2. This active code reassigns fred from 5 to 7 without printing out the initial value.
However, if we do not print fred the first time, the output is only 7 because the value of fred is just 7 when it is printed.
To picture this kind of multiple assignment you can think of a variable as the name of a container for values. When you assign a value to a variable, you change the contents of the container with that name, as shown in the figure:
An assignment of ’fred = 7’ changing the existing value of ’fred’.
Figure 3.7.3.
Using one variable to assign the value of another copies the value of the first variable into the second variable. This means that after the assignment, there are two copies of the same value. But the variables that hold those values are independent. Changing one will not change the other:
Listing 3.7.4.

Insight 3.7.1.

In mathematics, a statement of equality is true for all time. If \(a = b\) now, then \(a\) will always equal \(b\text{.}\) In C++, an assignment statement can’t permanently make two variables equal. All it can do it copy the current value from one variable to another.
Although multiple assignment is frequently useful, you should use it with caution. If the values of variables are changing constantly in different parts of the program, it can make the code difficult to read and debug. You generally should only use it when the same conceptual value changes. If you are programming a game, using multiple assignment to change the score variable every time the player scores points would make sense. But using multiple assignment to reuse variables for different purposes is generally a bad idea. Having a variable length that initially stores a value in inches and later is changed to store a value in centimeters would cause confusion for anyone trying to read the code.

Checkpoint 3.7.1.

What will print?
#include <iostream>
using namespace std;

int main() {
  int x = 10;
  cout << x << "!";
  x = 1;
  cout << x << "!";
}
  • 10!1!
  • There are no spaces between the numbers.
  • 10 ! 1 !
  • Remember, in C++ spaces must be printed.
  • 10 ! 10 !
  • Carefully look at the values being assigned.
  • Carefully look at the values being assigned.

Checkpoint 3.7.2.

What is the correct output? Draw a memory diagram and keep track of the current value of each variable while you mentally β€œrun” the code.
int x = 0;
x = 5;
int y = x;
y = 3;
bool z = x;
x = 10;
cout << z;
  • That is the value that x has when z was assigned
  • That is the value y has when z is assigned. It no longer has the same value as x.
  • That was the original value of x.
  • That is the value x gets after z has been assigned.
You have attempted of activities on this page.