Increment and decrement
It is common to find that you want to change a variable by adding or subtracting 1. Of course, n = n + 1;will change n to have a value one larger than its former value. But there are some convenient abbreviations. Statement n++;means the same thing as n = n + 1; and statement n--;is equivalent to statement n = n - 1; |
Operator assignment
Statement x += E;is equivalent to x = x + (E);For example, kangaroo += 2;is equivalent to kangaroo = kangaroo + 2; You can use any binary operator (+, -, *, etc.) with =. For example, statement x *= n + 1;has the same effect as statement x = x * (n+1); |
Do not use ++ or -- in expressions
It is common for students to confuse x++ with x+1.
But the two have very different meanings: x++ changes the value
of variable x, but x+1 does not change x.
The coding standards for this course require that you only use abbreviations such as x++ and x-- as statements, not as expressions. Never write x++ as a parameter of a function. |
Abbreviations for assignment statements include x++, which abbreviates x = x + 1, and x--, which abbreviates x = x - 1.
robinHood++;abbreviate? Answer
lazy *= w + 1;abbreviate? Answer
int x = 0; x = x++;make sense? If so, what is the value of x after doing both lines? Answer