Information Technology Reference
In-Depth Information
Increment and Decrement Operators
The increment operator adds 1 to the operand. The decrement operator subtracts 1 from the
operand. The operators and their descriptions are listed in Table 8-9.
These operators are unary and have two forms, the pre- form, and the post- form, which
act differently.
In the pre- form, the operator is before the operand; for example, ++x and --y .
￿
In the post- form, the operator is after the operand; for example, x++ and y-- .
￿
Table 8-9. The Increment and Decrement Operators
Operator
Name
Description
++
Pre-increment
++ Var
Increment the value of the variable by one and save it. Return the
new value of the variable.
Post-increment
Var ++
Increment the value of the variable by one and save it. Return the
old value of the variable before it was incremented.
--
Pre-decrement
-- Var
Decrement the value of the variable by one and save it. Return
the new value of the variable.
Post-decrement
Var --
Decrement the value of the variable by one and save it. Return
the old value of the variable before it was decremented.
In comparing the pre- and post- forms of the operators
￿
The saved value of the operand variable after the statement is executed is the same
regardless of whether the pre- or post- form of the operator is used.
￿
The only difference is the value returned by the operator to the expression.
An example summarizing the behavior is shown in Table 8-10.
Search WWH ::




Custom Search