5.1. Multiple assignment

I haven’t said much about it, but it is legal in C++ to assign to the same variable more than once. The effect of the second assignment is to replace the old value of the variable with a new value.

The code below 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.

The active code below 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.

This kind of multiple assignment is the reason I described variables as a container for values. When you assign a value to a variable, you change the contents of the existing storage location, as shown in the codelens below. Step through the code one line at a time and notice how the value assigned to fred changes even when no output is created.

Activity: CodeLens Reassigning values to fred (multiple_assignment_CL)

When there are multiple assignments to a variable, it is especially important to distinguish between an assignment statement and a statement of equality. Because C++ uses the = symbol for assignment, it is tempting to interpret a statement like a = b as a statement of equality. It is not!

Warning

An assignment statement uses a single = symbol. For example, x = 3 assigns the value of 3 to the variable x. On the other hand, an equality statement uses two = symbols. For example, x == 3 is a boolean that evaluates to true if x is equal to 3 and evaluates to false otherwise.

This is a common source of error.

First of all, equality is commutative, and assignment is not. For example, in mathematics if \(a = 7\) then \(7 = a\). But in C++ the statement a = 7; is legal, and 7 = a; is not.

Furthermore, in mathematics, a statement of equality is true for all time. If \(a = b\) now, then \(a\) will always equal \(b\). In C++, an assignment statement can make two variables equal, but they don’t have to stay that way!

int a = 5;
int b = a;     // a and b are now equal
a = 3;         // a and b are no longer equal

The third line changes the value of a but it does not change the value of b, and so they are no longer equal. In many programming languages an alternate symbol is used for assignment, such as <- or :=, in order to avoid confusion.

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 have attempted of activities on this page