Assigment Operator Problems

Given the following declarations, what are the new values of each variable after the given statement?

Work out the answers first, then click on the problem to see the answer. If you got it wrong, you'll be able to get an explanation indicating how the answer was determined.

Good Luck !!

int x = 10; int y = 15; int z = 6; int a, b, c;

1. x++; 2. --y; 3. a = ++z - 5; 4. b = 16 - y++; 5. c = ++x + z--;
6. x += 2 * y; 7. y -= x / --z; 8. z += x-- + 5; 9. y /= z + 2; 10. x *= ++y - z--;


Assignment Operator Problem Solutions

Click on "explain" to see how the answer in red was calculated


1. x++; yields x = 11

Explain


2. --y; yields y = 14

Explain


3. a = ++z - 5; yields a = 2 and z = 7

Explain


4. b = 16 - y++; yields b = 1 and y = 16

Explain


5. c = ++x + z--; yields x = 11, z = 5 and c = 17

Explain


6. x += 2 * y yields x = 40

Explain


7. y -= x / --z; yields y = 13 and z = 5

Explain


8. z += x-- + 5; yields x = 9 and z = 21

Explain


9. y /= z + 2; yields y = 1

Explain


10. x *= ++y - z--; yields x = 100, y = 16 and z = 5

Explain


Problem Explanations


Explanation of Problem 1

x++;

x++ is an example of post-increment. If x++ appears in an expression, we would use the current value of x in the expression, and then increment x afterwards. The same would be true in the case of post-decrement. In this case, however, there is no expression, so we simply increment x from 10 to 11. Used this way, x++; and ++x; are equivalent.


Explanation of Problem 2

--y:

--y; is an example of pre-decrement. If --y appears in an expression, we would first decrement y and use the new value of y in the expression. The same would be true in the case of pre-increment. In this case,however, there is no expression to evaluate, so we simply decrement y from 15 to 14. Used this way, --y; and y--; are equivalent.


Explanation of Problem 3

a = ++z - 5;

First, look at the expression on the right for any pre-increment and/or pre-decrement operators and perform them first. In this case, ++z is a pre-increment, so we increment z from 6 to 7. We then evaluate the expression... 7 - 5 is 2, which gets stored in a.


Explanation of Problem 4

b = 16 - y++;

Since the expression on the right has no pre-increment or pre-decrment operators, we use the current value of y to evaluate the expression. 16 - 15 = 1. So 1 is stored in b, then y is incremented from 15 to 16.


Explanation of Problem 5

c = ++x + z--;

Since ++x is pre-increment, we first increment x from 10 to 11 and use that value in the expression. Since z-- is post-decrement, we use the current value of z (which is 6) in the expression, then decrement z from 6 to 5. So the steps are:

  1. increment x from 10 to 11
  2. evaluate 11 + 6, which is 17 and store in c
  3. decrement z from 6 to 5


Explanation of Problem 6

x += 2 * y;

x += 2 * y; is equivalent to x = x + (2 * y);
Since y = 15, x = 10 + (2 * 15) = 10 + 30 = 40


Explanation of Problem 7

y -= x / --z;

We need to evaluate x / --z, then subtract that answer from y to get the new value of y. We know that "--" is the decrement operator, but we must pay attention to whether it's used as pre-decrement or post-decrement. In this case --z is pre-decrement, so we decrement z from 6 to 5 first, then we use the new value of z and evaluate x / 5. Since x is 10, x / 5 = 10 / 5 = 2. Finally, y -= 2; is equivalent to y = y - 2; so y = 15 - 2 which is 13.


Explanation of Problem 8

z += x-- + 5;

We first need to evaluate x-- + 5, then add that value to z
The fundamental question is whether we decrement x before (pre-decrement) or after (post-decrement) we use x's value in the calculation.
In this case, x-- is post-decrement, so we calculate x-- + 5 as 10 + 5 which is 15 then we decrement x from 10 to 9. Finally, z += 15 is equivalent to z = z + 15. Since z is currently 6, we get z = 6 + 15 which is 21.


Explanation of Problem 9

y /= z + 2;

This is one of the simpler problems, but reminds us about integer division.
Since z is 6, z + 2 = 8. y /= 8 is equivalent to y = y / 8. Since the value of y is currently 15, this becomes y = 15 / 8, which is 1 because we are doing integer division.


Explanation of Problem 10

x *= ++y - z--;

In order to evaluate ++y - z--, we must decide which values of y and z to use..... do we use the current values, or do we increment (decrement) first and use the new values. In this problem it's both. Because ++y is pre-increment, we first increment y from 15 to 16. Next look at z--. This is post-increment, so we use the current value of z, which is 6, then decrement z. So the steps are:

  1. increment y from 15 to 16
  2. use the current value of z (which is 6) and calculate 16 - 6 = 10 as the value of the expression
  3. calculate x *= 10 which is equivalent to x = x * 10. Since x is currently 10, this evaluates to x = 10 * 10 which is 100
  4. now decrement z from 6 to 5