Re: Funny loop with post-increment.

Jon Skeet <[email protected]> Tue, 19 Feb 2008 08:05:40 +0000
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <[email protected]>
Brady Kelly wrote:
> Why does this loop not exit?  On the first iteration, one would expect i ==
> 0, and that 0 to be assigned to i, and then i incremented.  Then, on the
> second iteration, i should be 1 before the assignment operation, as well as
> after the assignment operation, but i should be two after the
> post-increment, before the next assignment.
>
>
>             int i = 0;
>             while (i < 10)
>             {
>                 i = i++;
>             }

i=i++ is effectively this:

int tmp = i;
i = i + 1;
tmp = i;

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.

Jon

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

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