Increment and Decrement Operators

It is often needed to add 1 to or subtract 1 from a variable. The expression for doing this is
x = x + 1;
y = y - 1;
or
x += 1;
y -= 1;
The expression += 1 has a further shortened form of syntax, which is ++. It can be put in front of or after the variable, i.e., ++A, or A++. The effects of ++A and A++ on the variable A are same, but the values of the two expressions are different, as described below

If ++A and A- are two stand-alone expressions, their effects on the variable A are same and their own values are discarded, so they are equivalent. But if they are parts of a larger expression, their values will be different. For example:

>> n = 1;
>> p = ++n
   2
>> // now n is 2

>> q = n++;
>> // q is 2
>> // but n is now 3
>> q
    2
>> n
    3

oz 2009-12-22