+ 2

How exaclty do the x++ and ++x terms work? And how do they differ from each other?

22nd Nov 2017, 2:45 PM
Sam Lindauer
5 Respostas
+ 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.
22nd Nov 2017, 3:42 PM
Yonis Islam
Yonis Islam - avatar
+ 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
22nd Nov 2017, 3:30 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 2
post-increment(x++) and pre-increment(++x) is based upon when it increments, after or before the operation. Post = after, pre = before.
22nd Nov 2017, 2:53 PM
AgentSmith
+ 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 : )
22nd Nov 2017, 2:54 PM
#RahulVerma
#RahulVerma - avatar
+ 1
thanks everyone!! :D
22nd Nov 2017, 4:02 PM
Sam Lindauer