Question:
confusion between x++, ++x, y--, and --y?
shadi
2012-10-17 01:29:31 UTC
If y=5 then:-

1) x = ++y makes y=6 and x=6
2) x = y++ makes y=6 and x=5
3) x = --y makes y=4 and x=4
4) x = y—makes y=4 and x=5

wtf is going on? it makes no sense :-(

i have always known that y++ is shorthand for y=y+1. thus x=y++ becomes x = y+1 = 5 +1 = 6. so x=6 and y=6

also y-- is shorthand for y=y-1. thus x=y++ becomes x = y-1 = 5 -1 = 4. so x=4 and y=4

but that is wrong :-(

i read somewhere that:-
"++x increments the value of x and then returns x
x++ returns the value of x and then increments"

can someone plz explain what is going on and why i am wrong?
Four answers:
Chris D
2012-10-17 01:44:57 UTC
Fair questions.



The ++ and -- operators apply to the variable they're next to. By themselves, ++x and x++ have the same effect on x. (As do --x and x--.)



However, the expressions also "return" a value that can be used in a larger context. Consider this:

  y = ++x



What this means is "add one to x" and THEN "take the value of x (which has now been changed!) and assign it to y"



On the other hand, consider this:

  y = x++



What this means is "take the value of x and assign it to y" and THEN "add one to x".



The position of the ++ (or the --) indicates whether the increment (decrement) is to be applied to its variable before or after that *variable*'s value is passed onwards for further use.



Does this make better sense?
Socks
2012-10-17 08:43:36 UTC
Quoting you: "i read somewhere that:-

"++x increments the value of x and then returns x

x++ returns the value of x and then increments" "





So let's set x = 5, y = 7.



We type the command:

x = y++



There are in fact two commands being issued. The command to set x equal to y (x=y), and the command to increment the value of y (y++). These two commands are performed in order.

So x is set equal to 7.

THEN the value of Y is incremented:

y is set equal to 7+1, which is 8.

The end result is broken down as follows:

x = y++

Step 1: x = y // (x = 7)

Step 2: y++ // (y = 8)



Output:

x == 7

y == 8



With x=++y, they again happen in order, the ++ operator coming before y means we increment y first:

The value of y is incremented so y = 7 + 1 which is 8

Then we set x equal to the NEW current value of y.

Broken down as follows:

x = ++y

Step 1: y++ // y = 8

Step 2: x = y // x = 8



Output

x == 8

y == 8



Clear as mud?
walter_vos
2012-10-17 11:39:50 UTC
Depending on where the ++ is located in respect to y, the ++ gets implemented before or after the value is used in the rest of the operation.



x=y++ will increment after X is used -> x=0 y=1

(x becomes equal to y , afterwards y will be incremented by 1)



x=++y will increment before X is used -> x=1 y=1

(y will be incremented by 1 first, afterwards x becomes equal to y)
Emman
2012-10-17 10:42:54 UTC
x=1;

echo ++x; //outputs 2

echo x++; //outputs 1



echo x++; echo x; //outputs 2


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...