Barbara Ericson, Allen B. Downey, Jason L. Wright (Editor)
Section6.1Multiple assignment
I haven’t said much about it, but it is legal in 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 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 container, as shown in the figure:
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!
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.
First of all, equality is commutative, and assignment is not. For example, in mathematics if then . 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 now, then will always equal . 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.