What is the difference between x ++ and ++ x

Possible duplicate:
Increment in C ++ - When to use x ++ or ++ x?

What is the difference between x ++ and ++ x?

+6
c ++ javascript
source share
4 answers

x++ executes the statement and then increments the value.

++x increments the value and then executes the statement.

 var x = 1; var y = x++; // y = 1, x = 2 var z = ++x; // z = 3, x = 3 
+6
source share

++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 
+6
source share

x++ returns x and then increments it.

++x increments x, then returns it.

+4
source share

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.

If x is 1 , the first sets y to 2 ; the second sets y to 1 .

+1
source share

All Articles