+ 2
How exaclty do the x++ and ++x terms work? And how do they differ from each other?
5 odpowiedzi
+ 5
x++ is post increment it means you take the value of the variable without incrementing it then later you increment, and ++x is preincrement it means you first increment it.
+ 4
x++ does the increment in the next line
so if you say
int x=3;
cout<<x++;
it outputs 3;
but ++x does the increment on that line
so if you say
int x=3;
cout<<++x;
it outputs 4
+ 2
post-increment(x++) and pre-increment(++x) is based upon when it increments, after or before the operation. Post = after, pre = before.
+ 1
x++ is 'use then increment'
++x is 'increment then use'
Let's understand this more by an example : // using C++
int x = 2, y = 2, z
++x
cout<<x
// prints 3
y++
// currently value of y = 2
cout<<y
// prints 3
z = y
cout<<z
prints 3
// end each statement with semicolons : )
+ 1
thanks everyone!! :D