Increment means 'add to'. It often means 'add 1 to'. For starters, we will talk about adding 1 to the value held by a variable. We will be 'incrementing the value by 1', or 'incrementing the variable by 1', or often, simply, 'incrementing the variable'.
When you are counting by 1's with a variable, a very handy way to increment it by 1 is this way:
You can also add 1 to a variable, say x, in this way:
x += 1;
Note the 'plus equals' operator, +=, in this statement. This operator will take the current value of x and, in this case, add 1 to it.
You can increment a variable by any amount this way. For example, the following code changes the value of x by adding 5 to it:
x += 5;
There is another way to increase the value of a variable:
x = x + 1;
Here's how it works:
This method can also be used to add any value to x. Here's an example of adding 7 to x:
x = x + 7;
Subtracting from, or decrementing, a variable is a lot like incrementing a variable. First, a simple method to subtract 1 from a variable, say x:
Next, a method to subtract 1 or any other value from a variable:
x -= 1;
x -= 5;
Finally, you can decrement this way:
x = x - 1;
x = x - 7;
The example program demonstrates all the ways described here to increment and decrement a variable. Note the handy use of the printStatement command to output the values for x.
Next tutorial: Trigonometry