Lab 3: Experiment 9

Assignment Expressions

We have learned how to declare variables and how to input values for them via 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 expression.

Like input and output expressions, an assignment expression becomes a statement by appending a semicolon:

variable = expression;
Here, expression is any valid C++ expression and variable is an identifier that has been declared as a variable and whose type is compatible with that of expression.

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:

  1. expression is evaluated.
  2. The value of the expression is stored in the memory associated with 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.)

Assignment Is an Operator

All operators in C++ actually produce and return a value. Some operators also have a side effect. For example, the output operator << 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 i:

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?

Assignment Chains

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:

i = j = k = 78;
This is known as assignment chaining.

But what are the values of i, j, and k after the assignment?

Modify your program to declare i, j, and k, 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 i, j, and k. Compile and execute your program.

Question #3.9.2: What are the values of i, j, and k?

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 of i, j, and k in your program and attempt to compile and execute it. What (if any) values does the output statement produce for i, j, and k ?
The result probably seems rather confusing — only one of i, j, and k is changed!

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:

(i = (j = (k = 78)));
Do the inner parentheses first: assign 78 to k. Sounds good. As an expression, that assignment evaluates to 78, so we can also assign that value to j. Sounds even better. Is i any 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 i, j, and k?

Back to the Lab Exercise  |  Forward to the Next Experiment

Report errors to Larry Nyhoff (