Fun with Operators
-
Fun with Operators : int I = 6; I += I++; System.out.println("Result : " + I); // I is now 12 int I = 6; I += ++I; System.out.println("Result : " + I); // I is now 13
You've just described the semantics of the postfix and prefix version of '++'. Do you claim it is a subtle bug in the Java language? :~
-- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!
-
Fun with Operators : int I = 6; I += I++; System.out.println("Result : " + I); // I is now 12 int I = 6; I += ++I; System.out.println("Result : " + I); // I is now 13
Wrong! They're both 13 I had a look and knew something wasn't right, so I ran the conspicuous one in PHP. This is right... int I = 6; I += I++; System.out.println("Result : " + I); // I is now 13 int I = 6; I += ++I; System.out.println("Result : " + I); // I is now 13 However...
// PHP $i = 6; echo ($i = $i + $i++); echo "Result : " . $i;
Displays 12 twice :omg: Not good. I think the first line should display 12 and second 13. I wonder how it would be displayed when compiled in other languages.// PHP $i = 6; echo ($i = $i + ++$i); echo "Result : " . $i;
Displays 13 twice, this one makes sense; :sigh: any one with something to say about PHP must come through me :suss: heheh ;P -
You've just described the semantics of the postfix and prefix version of '++'. Do you claim it is a subtle bug in the Java language? :~
-- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!
-
Wrong! They're both 13 I had a look and knew something wasn't right, so I ran the conspicuous one in PHP. This is right... int I = 6; I += I++; System.out.println("Result : " + I); // I is now 13 int I = 6; I += ++I; System.out.println("Result : " + I); // I is now 13 However...
// PHP $i = 6; echo ($i = $i + $i++); echo "Result : " . $i;
Displays 12 twice :omg: Not good. I think the first line should display 12 and second 13. I wonder how it would be displayed when compiled in other languages.// PHP $i = 6; echo ($i = $i + ++$i); echo "Result : " . $i;
Displays 13 twice, this one makes sense; :sigh: any one with something to say about PHP must come through me :suss: heheh ;P"// PHP $i = 6; echo ($i = $i + $i++); echo "Result : " . $i; Displays 12 twice Not good. I think the first line should display 12 and second 13. I wonder how it would be displayed when compiled in other languages." I have Your bug by the balls, so to say :-) Here's how it works: a) "$i + $i" evaluates to 12 b) $i++ evaluates to 7 c) $i evaluates to 12 in other words, the post-incremenation comes BEFORE the assignment, so $i becomes 12 and not 13 (it's 6->7 then 12, not 12 then 12->13). The second example works, because ++$i evaluates to 7 first. The difference between pre- and post-incrementation is that the former increments before reading the variable, and the latter increments AFTER the value is taken into consideration in whatever computation. It's the same with pre- and post-decrementation. Actually, this is a textbook example of "not getting" these operators. :)
-
"// PHP $i = 6; echo ($i = $i + $i++); echo "Result : " . $i; Displays 12 twice Not good. I think the first line should display 12 and second 13. I wonder how it would be displayed when compiled in other languages." I have Your bug by the balls, so to say :-) Here's how it works: a) "$i + $i" evaluates to 12 b) $i++ evaluates to 7 c) $i evaluates to 12 in other words, the post-incremenation comes BEFORE the assignment, so $i becomes 12 and not 13 (it's 6->7 then 12, not 12 then 12->13). The second example works, because ++$i evaluates to 7 first. The difference between pre- and post-incrementation is that the former increments before reading the variable, and the latter increments AFTER the value is taken into consideration in whatever computation. It's the same with pre- and post-decrementation. Actually, this is a textbook example of "not getting" these operators. :)
-
The C specification (and C++, I believe) state that this the result is undefined. So the compiler can produce any result and still be "correct". I've never seen the spec for PHP, assuming there is one.
I'm not sure either if there is a spec for PHP - I just read the manual and learn as I go. ;) As for the inc/dec operators, I've heard a discussion about which one was more efficient when used as a standalone operation, ie.
... i++; doSomething(); ...
or... ++i; doSomething(); ...
and from what I remember, someone said that pre-inc/dec was more efficient, because it doesn't copy the value somewhere... I'm not really that good with C/C++, but with PHP no matter how many times I run what I've posted before it ALWAYS evaluates the way I expect it to. I guess that's what the whole hype's about with deterministic computing, etc. ... ;) But You are right in that we should be more explicit about what we want to do, and what we expect to get in return. I mean, ie. the strict-comparison (===) operator exists for a reason... :) Besides, that whole line ( $i = $i + $i++; ) is just plain wrong, as in 'close to obfuscated'... not that I've never done a similar thing, one way or the other... :-O -- modified at 23:06 Tuesday 3rd October, 2006 -
"// PHP $i = 6; echo ($i = $i + $i++); echo "Result : " . $i; Displays 12 twice Not good. I think the first line should display 12 and second 13. I wonder how it would be displayed when compiled in other languages." I have Your bug by the balls, so to say :-) Here's how it works: a) "$i + $i" evaluates to 12 b) $i++ evaluates to 7 c) $i evaluates to 12 in other words, the post-incremenation comes BEFORE the assignment, so $i becomes 12 and not 13 (it's 6->7 then 12, not 12 then 12->13). The second example works, because ++$i evaluates to 7 first. The difference between pre- and post-incrementation is that the former increments before reading the variable, and the latter increments AFTER the value is taken into consideration in whatever computation. It's the same with pre- and post-decrementation. Actually, this is a textbook example of "not getting" these operators. :)
Egon_Freeman wrote:
the post-incremenation comes BEFORE the assignment
So strange anyway, what is the compiler outputing to machine code? We have 2 operations in one line, A and B. B can be ($i++) Low Level Code: 1. Evaluate i+i to a new memory address A2 2. Evaluate i++ to a new memory address B2 being 7 3. Assign i the value of B2 4. Assign i the value of A2 :confused: isn't this backwards Ahh I get it, ;P 1. Evaluate i+i to a new memory address A2 (i+i) 2. Do i++ (but NOT to a new memory address) actually increment the contents of that memory location. (This is probably possible with one line of assembly) 3. Assign i the value of A2 (i =) :-> Still dumb if you ask me heh. Definitely a subtle bug I think. Best to always keep things on sepatate lines, that's what I do.:cool:
-
You've just described the semantics of the postfix and prefix version of '++'. Do you claim it is a subtle bug in the Java language? :~
-- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!
Well, atleast in C++, the semantics are undefined if the prefixed or postfixed variable is used elsewhere in the same expression.
Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
I'm not sure either if there is a spec for PHP - I just read the manual and learn as I go. ;) As for the inc/dec operators, I've heard a discussion about which one was more efficient when used as a standalone operation, ie.
... i++; doSomething(); ...
or... ++i; doSomething(); ...
and from what I remember, someone said that pre-inc/dec was more efficient, because it doesn't copy the value somewhere... I'm not really that good with C/C++, but with PHP no matter how many times I run what I've posted before it ALWAYS evaluates the way I expect it to. I guess that's what the whole hype's about with deterministic computing, etc. ... ;) But You are right in that we should be more explicit about what we want to do, and what we expect to get in return. I mean, ie. the strict-comparison (===) operator exists for a reason... :) Besides, that whole line ( $i = $i + $i++; ) is just plain wrong, as in 'close to obfuscated'... not that I've never done a similar thing, one way or the other... :-O -- modified at 23:06 Tuesday 3rd October, 2006