+ 2
How exactly work the increment operator?
I don't understand how work ++x and x++
3 Réponses
+ 4
Ianel Tombozafy
Post Increment => x++
Pre Increment => ++x
Post means after, so Post Increment means "Increase after"
Example
========
int x = 2;
int y = x++ ;
This statements simply says
"Assign the value of x(2) to y, THEN increment x by 1 after the statement So that y= 2, x =3"
Pre means before, so Pre Increment means
"Increase first, then carry out the statement"
Example
========
int x = 3
int y = ++x
The statement simply says
"First of all increase "x" by 1, then add the new value of "x" to "y", so that
x= 3, y= 3 as well "
+ 1
Ianel Tombozafy
x++ : Use x, then increment it by 1.
++x: Increment x by one, then use x.
+ 1
Ok thanks