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.
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.
-
Q-4: Match the expression to the statement that best describes it.
Try again!
- 4=a
- Illegal
- a==b
- Checking if a is equal to b
- a=b
- Assigning a to the value of b
- a=4
- Setting the value of a to 4
- 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.
- 1!1!
- Carefully look at the values being assigned.
Q-5: What will print?
#include <iostream>
int main () {
int x = 10;
std::cout << x << "!";
x = 1;
std::cout << x << "!";
return 0;
}
- True
- Remember that printing a boolean results in either 0 or 1.
- False
- Remember that printing a boolean results in either 0 or 1.
- 0
- Is x equal to y?
- 1
- x is equal to y, so the output is 1.
Q-6: What is the correct output?
#include <iostream>
using std::cout;
int main () {
int x = 0;
x = 5;
int y = x;
y = 5;
bool z = x == y;
cout << z;
}