can you increment i?
Consider the following code (from this thread on SPF):
$i = 1;
$i += ++$i + $i++;
print $i;
What will this code output?
Make a guess, and then go on reading the rest of this post.
[ this space left blank, take your time to guess :) ]
The answer is: it depends on the language that processes the code!
In PHP, it would output 7. In Perl, the same code would output 8. In Java, it would output 5.
What was your guess?
29 responses
I was way off. My guess was the capital of Spain. ;^p
#1 Davezilla — 2002/11/07 at 22:21
I guessed 7, but only after some severe head scratching and counting-on-fingers. I don’t get why Perl and Java would output 8 though.
#2 Simon Willison — 2002/11/07 at 23:11
Oops, Java actually outputs 5. When editing the post, I changed the order of the ‘Perl’ and ‘Java’ parts and forgot to place Java’s output at its new place.
I guessed 6, because I forgot $i would get incremented once after the operation is done.
Dave, ‘Madrid’ isn’t the right answer, but I heard VBasic outputs ‘jesusatemyballs’. Why it would output a string, and why it had to be that string, is a mystery that’s never been solved so far, and this is why VBasic coders never increment $i.
#3 michel v — 2002/11/08 at 0:10
I guessed 7. My buddy Scott told me that it gives 3 on g++.
#4 chunshek — 2002/11/08 at 4:46
I guessed 6 :-/
#5 PapaScott — 2002/11/08 at 18:24
I read that thread a while ago. Very interesting isn’t it? :)
#6 Daynah — 2002/11/08 at 18:30
I guessed 5. 1 + 2 + 2;
#7 philip — 2002/11/09 at 3:02
I guessed 7, but I have no idea if the reasoning behind this guess is correct. It would be interresting to know WHY the result is 7
#8 Tobias — 2002/11/10 at 14:01
I guessed 8 : 3 += 2 + 3.
To me, it should be operated in the following order :
$i += ++$i + $i++;
[3] [1] [2]
[1] (++i) is equivalent to (1 + 1), $i is now equal to 2;
[2] ($i++) is equivalent to (2 + 1), $i is now equal to 3;
[3] ($i += ++$i + $i++) is therefore equivalent to ($i = 3 + 2 + 3), $i in the end is equal to 8.
I must be missing something here, but what ?
#9 skamp — 2002/11/10 at 18:28
This is how I see it operated (at least in PHP):
$i += ++$i + $i++;
[2] [1] [3]
2 += 2 + 2
and then $i++, which is executed after the mathematical statement, makes that 6+1 = 7 :)
Your explanation is maybe how Perl works with that line.
#10 michel v — 2002/11/10 at 18:41
OK, a quick reading of PHP’s documentation got me the answer concerning the output in that language.
In PHP, the expression is evaluated in the following order :
++$i -> $i = 2
$i++ -> $i = 3
$i + $i -> 3 + 3
$i += -> 1 + 3 + 3
http://docs.skamp.net/php/language.operators.html
http://docs.skamp.net/php/language.operators.increment.html
#11 skamp — 2002/11/10 at 18:43
OK, now I’m confused :-/
#12 skamp — 2002/11/10 at 18:45
Don’t be confused, I may be totally wrong. It all comes down to this question: is the equal sign a sequence stop in PHP? It is not in C, AFAIK.
#13 michel v — 2002/11/10 at 18:47
To me, that’s the way it works:
$i += ++$i + $i++
[4] [1] [3] [2]
[1] i is incremented => $i == 2
[1b] i is evaluated for the addition => $i += 2 + $i++
[2] i is evaluated for the addition => $i += 2 + 2
[2b] i is incremented right after being evaluated => $i == 3
[3] 2 + 2 = 4, so now we have $i += 4, and $i == 3
[4] 3 + 4 = 7
Ok, it’s not very clearly explained, but I’m late for an appointment, sorry :)
#14 garoo — 2002/11/10 at 19:24
That’s the trap, Garoo! :)
In fact, 7 is not the result that comes right from the addition. For example if you do “print ($i + ++$i + $i++)”, it will display 6, and then after that statement $i will equal 7.
#15 michel v — 2002/11/10 at 19:28
i++, ++i, +=i
can you increment i?
#16 RoyalTS - without cheese — 2002/11/11 at 0:02
That’s the trap, Michel! :))
Try this:
$i = 1; print ($i + $i++);
$i = 1; print ($i++ + $i);
If you were right, both lines would give the same result, because the ++ would be interpreted after the print.
Yet they don’t, because the ++ is interpreted right after the $i++ is evaluated.
#17 garoo — 2002/11/11 at 19:12
garoo: the problem lies with the pre-incrementation stuff, not the post.
#18 totalementcretin — 2002/11/12 at 1:03
It depends on C code optimisations too ;o)
if you compile:
main() {
volatile int i;
i = 1;
i += ++i + i++;
printf(”result: %i\n”, i);
}
you’ll see it returns: 3
and if you consider
register int i;
it gives: 7
#19 The Through — 2002/11/13 at 16:58
I guessed 6. ;)
#20 Christina — 2002/11/16 at 19:49
i guessed 7 :)
#21 Ling — 2002/11/19 at 1:50
Ah! Oh!
And I didn’t guessed anything! %-(
#22 Max — 2002/11/22 at 18:53
I quickly guessed 5 and tested it out in C++ (using a plain old int and compiling with g++). The answer turned out to be 7. If I think about it some more, it will probably make sense.
/me squirts some WD-40 onto the folds of her brain
#23 Rydain — 2002/11/28 at 9:42
Whoa…I’m having a headache. Keep those in the source. >:O
#24 Cessy — 2002/12/07 at 3:25
Got 5, as c-code should have given..
Renee.
#25 Renee Teunissen — 2002/12/10 at 0:29
I guessed 6 :(
i starts as 1, then it’s added to it’s increment thus 2, but i++; is not counted in the addtion until AFTER the statement, so I got 1 += 2 + 2, thus 5, then the i++ kicks in, giving 6 =\
#26 Qingy — 2003/01/03 at 18:37
[…] p;#8220;i” $i = 1; $i += ++$i + $i++; print $i; For the answer look at the Original post Comments (0) | TrackBack (0) | PingBack (0) Time for this page: 0.054 […]
#27 blog.tukker.org — 2003/01/20 at 14:26
Oddly, I guess 6.
#28 melz — 2003/01/30 at 0:12
Hi! I tried the code on C and C++ both, and its same in both cases.
int main()
{
int value,i=1;
value=i + i++ + ++i;
printf(”%d”,value);
i=1;
i += i++ + ++i;
printf(” %d”,i);
return 0;
}
Next i tried it with both volatile and without volatile keyword on “i”. Now the catch is:-
1) Without volatile, i.e. with optimization, the answer is 4 7
2) With Volatile, i.e. without optimization, the answer is 4 5
**Will anybody make it clear why the answer in first part is 7 and in other is 5, what effect does optimization have on this???
#29 Rahul — 2007/10/05 at 21:07
Your words