UMBC CMSC 201 Spring '06 CSEE | 201 | 201 S'06 | lectures | news | help |

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--; |

**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,

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

**x += 2 * y;**

x += 2 * y; is equivalent to x = x + (2 * y);

Since y = 15, x = 10 + (2 * 15) = 10 + 30 = 40

**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**,

**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.

**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.

**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:

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

CSEE | 201 | 201 S'06 | lectures | news | help

Monday, 23-Jan-2006 09:13:41 EST