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.
1#include <iostream>
2
3int main () {
4 int fred = 5;
5 std::cout << fred;
6 fred = 7;
7 std::cout << fred;
8 return 0;
9}
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.
1#include <iostream>
2
3int main () {
4 int fred = 5;
5 fred = 7;
6 std::cout << fred;
7 return 0;
8}
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.
1#include <iostream>
2
3int main () {
4 int fred = 5;
5 std::cout << fred << '\n';
6 fred = 8;
7 fred = 13;
8 fred = 21;
9 fred = 34;
10 std::cout << fred << '\n';
11 return 0;
12}
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.
Q1
Match the expression to the statement that best describes it.
Q2
What will print?
#include <iostream>
using namespace std;
int main () {
int x = 10;
cout << x << '!';
x = 1;
cout << x << '!';
return 0;
}
Q3
What is the correct output?
#include <iostream>
using namespace std;
int main () {
int x = 0;
x = 5;
int y = x;
y = 5;
bool z = x == y;
cout << z;
}