Some assignment operations are performed so frequently that C++ provides special operators for them. For example, instead of writing
C++ provides thesum = sum + value;
+=operator that allows us to write an equivalent statement more succinctly:
Similarly, to double the value insum += count;
result, we can write
or we can more succinctly write:result = result * 2;
(Of course, each of the variables here would have to be declared first, probably asresult *= 2;
Such shortcut operators save us from having to retype the same
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:
can be written as
Each of these shortcut operators can be chained in the same manner as a normal assignment.
Suppose we had variables
m all initialized as follows:
Since these assignment shortcuts are still assignment operators, they have the same associativity as the regular assignment operator. So consider this expression:int i = 8, j = 4, k = 2, m = 1;
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?
Assignment statements that increment an integer variable by 1,
i = i + 1
or decrement it by 1,
are used so much in programming that special increment and decrement operators have been included in C++:
i = i - 1
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
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:++i
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 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
ibefore the increment? What value is assigned to
postI? What is the value of
iafter 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,
++j. Since it's a pre increment, we increment
the variable first, then we take its new value as the value of
Question #3.10.4: Predict: what is the value of
jincremented? What value is assigned to
preJ? What is the value of
jafter 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.