Possible duplicate:Increment in C ++ - When to use x ++ or ++ x?
What is the difference between x ++ and ++ x?
x++ executes the statement and then increments the value.
x++
++x increments the value and then executes the statement.
++x
var x = 1; var y = x++; // y = 1, x = 2 var z = ++x; // z = 3, x = 3
++x higher in order of operations than x++ . ++x occurs before the assignment, but x++ occurs after the assignment.
For exmaple:
var x = 5; var a = x++; // now a == 5, x == 6
and
var x = 5; var a = ++x; // now a == 6, x == 6
x++ returns x and then increments it.
++x increments x, then returns it.
If you write y = ++x , the variable y will be assigned after increasing x .If you write y = x++ , the variable y will be assigned before x incremented.
y = ++x
y
x
y = x++
If x is 1 , the first sets y to 2 ; the second sets y to 1 .
1
2