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
int
s.)
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:
can be written asVarable1
=Variable1
X
Expression
Each of these shortcut operators can be chained in the same manner as a normal assignment.Varable1
X
=Expression
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?
C programmers were so lazy, that even typing
i += 1instead of
i = i + 1was 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
++iThe 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:
i++
, changes the variable after it is
evaluated as an expression.++i
, changes the variable before it is evaluated as
an expression.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 ofi
before the increment? What value is assigned tovalI
? What is the value ofi
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 ofj
incremented? What value is assigned tovalJ
? What is the value ofj
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.