Incrementing A Variable

There are 4 ways to make a variable one larger or one smaller:

int x = 10;

//increment
x = x + 1;
x += 1;
x++;
++x;

//decrement
x = x - 1;
x -= 1;
x--;
--x;
The last two in each group above are new to you. Let's take the increments. Both
x++;
++x;
make x one larger. But these are also expressions. And the expressions have different values. Consider:
int x;

x = 10;
int y = x++;

x = 10;
int z = ++x;
The value of both increments is the value of x, but the value of the post increment, x++, is the value of x before the increment takes place, while the value of the pre increment, ++x, is the value of x after the increment. Thus y has a 10 in it and z has an 11 in it.

The post and pre decrements work in a similar fashion. We will use the post and pre increments and decrements when we need to change the value of a variable by 1.

next: More Loops