Increment (++) and Decrement (--) Operators
Increments or decrements the value of a variable by one.
//prefix syntax ++variable --variable //postfix syntax variable++ variable--
The increment and decrement operators are used as a shortcut to modify the value stored in a variable and access that value. Either operator may be used in a prefix or postfix syntax.
| If | Equivalent Action | Return value |
|---|---|---|
| ++variable | variable += 1 | value of variable after incrementing |
| variable++ | variable += 1 | value of variable before incrementing |
| --variable | variable -= 1 | value of variable after decrementing |
| variable-- | variable -= 1 | value of variable before decrementing |
The following example illustrates the difference between prefix and postfix syntax for the ++ operator.
// Example of prefix increment operator var j1 : int = 2; var k1 : int; k1 = ++j1; // k1 is 3, the value of j1 after incrementing // Example of postfix increment operator var j2 : int = 2; var k2 : int; k2 = j2++; // k2 is 2, the value of j2 before incrementing
Show: