HTML and CSS Reference
In-Depth Information
Now you have four ways to add 1 to the value of a variable:
x += 1;
x += ;
x++;
++x;
and four ways to subtract 1 from the value of a variable:
x = x - 1;
x -= 1;
x- -;
- -x;
Refer to Table 5.4. In section “Loops” on page 131, you'll see these operators are com-
monly used to increment or decrement loop counters.
Table 5.4 Autoincrement and Autodecrement Operators
Operator
Function
What It Does
Example
++x
Pre-increment
Adds 1 to x
x = 3; x++;
x is now 4
x++
Post-increment
Adds 1 to x
x = 3; ++x;
x is now 4
- -x
Pre-decrement
Subtracts 1 from x
x = 3; x - -;
x is now 2
x- -
Post-decrement
Subtracts 1 from x
x = 3; - -x;
x is now 2
Autoincrement and Autodecrement Operators and Assignment. The place-
ment of the operators does make a difference in more complex expressions, especially
when part of an assignment; for example, y = x++ is not the same as y = ++x .
Start with: y = 0 ; x = 5;
before
0
y
5
x
Pre-increment
after
6
y
6
x
y = ++x;
before
0
y
5
x
Post-increment
after
5
y
6
x
y = x++;
Search WWH ::




Custom Search