Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Increment (++) and Decrement (--) Operators 

Increments or decrements the value of a variable by one.


//prefix syntax
++variable
--variable
//postfix syntax
variable++
variable--
variable

Any numeric 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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.