Lab 3: Experiment 10


Assignment Shortcuts

There are some assignment operations that are performed so commonly, 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 X is an arithmetic operator, then an expression of the form:

Varable1 = Variable1 X Expression
can be written as
Varable1 X= 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 like so:

int i = 8, j = 4, k = 2, m = 1;
Since these assignment shortcuts are still assignment operators, they have the same associativity as the plain old 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 print out the individual variables, not the whole assignment expression.

Question #3.10.2: What are the values of each variable following the assignment expression in your program?

Increment and Decrement Expressions

C programmers were so lazy, that even typing

i += 1
instead of
i = i + 1
was too much for them. So, they created special increment and decrement operators: ++ and --, respectively. We'll take a look at the increment operator, but the decrement operator really isn't any different except that we subtract one instead of add one when we invoke the side effect.

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, particularly 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 where it matters. Here's the difference:

So consider this code:

int i = 3, j = 5;
int valI = i++;
int valJ = ++j;

In the declaration of valI, 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 valI? What is the value of i after all the code is executed?

Similarly, in the declaration of valJ, 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 valJ? 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.

Terminology

decrement operator, increment operator, post increment, pre increment
Back to the Lab Exercise
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.