We have learned how to declare variables and how to input values for them
cin (as in Experiment #2).
We look now in more detail at the most commonly used way to change the value
of a variable — through an assignment
Like input and output expressions, an assignment expression becomes a statement by appending a semicolon:
expressionis any valid C++ expression and
variableis an identifier that has been declared as a variable and whose type is compatible with that of
We see that this has the same form as a variable declaration but without the type specifier that specifies the type of the variable. However, there is a basic difference between them: a declaration is used at compile time to allocate memory for a variable, and an assignment is processed during execution and modifies the memory location for that variable. When execution reaches an assignment statement:
variable, replacing any value that may already be stored there.
For now, we will declare each variable one time, just before the first time it is used, and then use assignment statements to change its value. (There are situations where the same identifier is used in more than one declaration, but we'll wait untiil later to discuss that.)
All operators in C++ actually produce and return a value. Some
operators also have a side effect. For example, the
<< evaluates to an output stream.
Changing the contents of the screen is a side effect.
Similarly, an assignment operator has a side effect: change the contents of a variable. But it also has a value. We can find out what that value is by displaying assignment expressions.
Add the following output statement that contains three assignment
expresions to your program, after your declaration of
cout << "555: " << ( i = 555 ) << endl << "-36: " << ( i = -36 ) << endl << "2+3: " << ( i = 2+3 ) << endl;
Question #3.9.1: What does this output statement display?
Because the assignment symbol
= is an actual operator that
produces a value in addition to having a side effect, we can chain
assignments as in the following statement:
This is known as assignment chaining.i = j = k = 78;
But what are the values of
Modify your program to declare
initializing them to different values less than 10.
Then put the above assignment chain right after this declaration followed
by a statement to output the values of
k. Compile and execute your program.
Question #3.9.2: What are the values of
The result may surprise you. Let's analyze it more closely.
The other operations we've considered have all been left-associative. So one
would think that the assignment operator
= should also be.
This means that the compiler would implicitly parenthesize the expression as
(((i = j) = k) = 78);
Question #3.9.3: Enter this statement followed by a statement to output the values ofThe result probably seems rather confusing — only one of
kin your program and attempt to compile and execute it. What (if any) values does the output statement produce for
So assignment is not left associative; and because "middle associative" would be really unusual (and too often ambiguous), it seems that it must be right associative:
Do the inner parentheses first: assign 78 to(i = (j = (k = 78)));
k. Sounds good. As an expression, that assignment evaluates to 78, so we can also assign that value to
j. Sounds even better. Is
iany different? Let's try it out.
Explicitly parenthesize the assignment chain so it is right associative. Recompile and execute the code.
Question #3.9.4: What are the values of