Increment | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Increment

I have a doubt, I'm confuse, the statement x = x + 1; is not the same as x++? In the increment and decrement course, it says that ++x is the same as x = x +1, so... which one is equal to x = x + 1? x++ or ++x

17th Apr 2017, 9:57 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
5 Answers
+ 8
x = x+1 is the same as ++x. Example: int x = 1; if((x=x+1) == 2) print("++x"); else print("x++"); print(x); output: ++x2. note* x = x+1 had to be surrounded in parentheses. The ++x, and x++ incrementation can fit the wants of the programmer better, as it's easier to just say x++ if I don't want to increment it yet.
17th Apr 2017, 10:31 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
yes it's the samething. there's also: x += 1
17th Apr 2017, 10:01 PM
Welliton Malta
Welliton Malta - avatar
0
x++ and ++x both increment the value of x. the logic I use is that if ++ is before x, then the increment has priority. If x comes before, then its initial value has priority and it'll be incremented after that operation.
17th Apr 2017, 11:54 PM
Andrea Barba
Andrea Barba - avatar
0
x++ or x+1 r not same....Because​ let,x=2; when u write x=x+1; then the output will show 2+1=3; but when u write x++; in a loop the value of x will be 2 because 1st assignment of the value of x and then increment of the value of x..for this next loop the the value of x will be taken as 3....thn increment value will be 4....
18th Apr 2017, 1:19 AM
sudip mishra
sudip mishra - avatar
0
++x= 1st increment then assignment the value and x++ = 1st assignment then increment the value program always shows you the assignment value...this is the difference between x++ & ++x...
18th Apr 2017, 1:23 AM
sudip mishra
sudip mishra - avatar