Friday, February 25, 2011

The final four statements actually update the value stored in the "a" variable as well as the one on the left of the assignment operator. I will explain how this works by assuming that "a" starts off as 5 in each case. The statement b = ++a first adds 1 to the value in "a" and then assigns that value to "b" so both variables now contain 6. The statement c = a++ first assigns the value from "a" into "c" and then adds 1 to "a" so now "c" contains 5 and "a" contains 6. The statement d = --a subtracts one from "a" and then assigns the result to "d" so both variables now contain 4. I'll leave it for you to figure out what values "a" and "e" end up with after the last statement executes.

Combination Operators

Next we'll look at some more assignment operators. Like the basic assignment operator these also assign a new value to the variable to the left of the operator but with these the original value is not lost but is used in calculating the new value to be stored. Here are some examples:

No comments:

Post a Comment