Lab 3: Experiment 10


Assignment Shortcuts

Some assignment operations are performed so frequently that C++ provides special operators for them. For example, instead of writing

sum = sum + value;
C++ provides the += operator that allows us to write an equivalent statement more succinctly:
sum += count;
Similarly, to double the value in result, we can write
result = result * 2;
or we can more succinctly write:
result *= 2;
(Of course, each of the variables here would have to be declared first, probably as ints.)

Such shortcut operators save us from having to retype the same identifiers (sum and result) twice, and C++ provides such a shortcut for each of the arithmetic operators. That is, if Δ is an arithmetic operator, then an expression of the form:

varable = variable Δ expression
can be written as
varable Δ=  expression
Each of these shortcut operators can be chained in the same manner as a normal assignment.

Suppose we had variables i, j, k, and m all initialized as follows:

int i = 8, j = 4, k = 2, m = 1;
Since these assignment shortcuts are still assignment operators, they have the same associativity as the regular assignment operator. So consider this expression:
i -= j /= k *= m += 1

Question #3.10.1: Make a prediction: what are the values of each variable following the assignment expression?

Modify your program to see if you're right. Be sure to output the individual variables, not the whole assignment expression.

Question #3.10.2: In your program, what are the values of each variable following the assignment expression?

Increment and Decrement Expressions

Assignment statements that increment an integer variable by 1,

i = i +  1

or decrement it by 1,

i = i - 1
are used so much in programming that special increment and decrement operators have been included in C++:

++  and  --
respectively. We'll consider the increment operator here, because the decrement operator behaves in basically the same way except that we subtract one instead of add one.

Instead of the expressions above, we can simply write

i++
or
++i
The increment operator only needs one operand, and we get a choice where to put the operator, before or after the operand. Most of the time — in particular, when used as a single statement — it doesn't matter if the increment is before or after the variable. It's only in a context that it matters:

So consider this code:

int i = 3, j = 5;
int postI = i++;
int preJ = ++j;

In the declaration of postI, we must first evaluate the expression on the right side of the assignment operator, specifically i++. Since it's a post increment, the value of the expression is the original value of the variable. Then we increment the variable.

Question #3.10.3: Predict: what is the value of i before the increment? What value is assigned to postI? What is the value of i after all the code is executed?

Similarly, in the declaration of preJ, we must first evaluate the expression on the right side of the assignment operator, specifically ++j. Since it's a pre increment, we increment the variable first, then we take its new value as the value of the expression.

Question #3.10.4: Predict: what is the value of j incremented? What value is assigned to preJ? What is the value of j after all the code is executed?

Test your predictions. Use the code above and an output statement to display the values for all of the variables.

Question #3.10.5: What did your program display for these variables? Compare your predictions to the actual results.


Back to the Lab Exercise


Report errors to Larry Nyhoff (nyhl@cs.calvin.edu)