AP Computer Science Java: Lesson 3.2
Repetitive Statements - Increment Operators


Lesson 3.2 - Increment Operators

Purpose: To learn how to add values to other variables in short Java notation

Increment?  
OK, so not your everyday, household word. What it means is "the act or process of increasing." We are incrementing a variable in Java when we add to it. Such statements as x++ or x+=5 are examples. These are shorthand versions of possibly more familiar statements. Let's look at some examples of incrementation shorthand along with similar versions for operations other than adding. Note that in the following expressions the variable x can be of type int or double.

Short Version Long Version What it means
x++
x = x + 1
add 1 to x
x--
x = x - 1
subtract 1 from x
x+=5
x = x + 5
add 5 to x
x-=5
x = x - 5
subtract 5 from x
x*=5
x = x * 5
multiply x by 5
x/=5
x = x / 5
divide x by 5


In closing, You should be familiar with both the short and long versions of these statements as you will see and use them frequently. Most of the time, they are used in settings requiring loops - both for and while.


© DanShuster.com