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

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.

+ 2

yes it's the samething. there's also: x += 1

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.

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....

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...