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
++A will add 1 to the value of A, and return the updated value of A. -A will subtract 1 from the value of A, and return the updated value of A.
++A will add 1 to the value of A, and return the original value of A. -A will subtract 1 from the value of A, and return the original value of A.
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