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 !!
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--; |
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
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.
--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.
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.
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.
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:
x += 2 * y;
x += 2 * y; is equivalent to x = x + (2 * y);
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.
z += x-- + 5;
We first need to evaluate x-- + 5, then add that value to z
Explanation of Problem 1
Explanation of Problem 2
Explanation of Problem 3
Explanation of Problem 4
Explanation of Problem 5
Explanation of Problem 6
Since y = 15, x = 10 + (2 * 15) = 10 + 30 = 40
Explanation of Problem 7
Explanation of Problem 8
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.