Re: Funny loop with post-increment.

Jon Skeet <[email protected]> Tue, 19 Feb 2008 08:24:04 +0000
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <[email protected]>
Frans Bouma wrote:
>> In other words, the increment happens after the evaluation of i, but
>> before the assignment - so the assignment just sets it back to the
>> original value.
>
>         I don't find this obvious. Right-hand side of the assignment should be
> evaluated first, so:
> i=i+1
> i=i
>

The RHS of the assignment *is* evaluated first - the result of "i++" is
the value *before* the increment. So, when i = 0 the value of the
expression "i++" is 0. That value is then assigned to i.

 >         I don't see how /why a temp variable is suddenly created.

 From the spec (7.5.9)

<quote>
•     If x is classified as a variable:
o       x is evaluated to produce the variable.
o       The value of x is saved.
o       The selected operator is invoked with the saved value of x as its
argument.
o       The value returned by the operator is stored in the location given by
the evaluation of x.
o       The saved value of x becomes the result of the operation.
</quote>

Here I'm just using tmp to be "the saved value".


Jon

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com