Re: Funny loop with post-increment.

Frans Bouma <[email protected]> Tue, 19 Feb 2008 09:14:50 +0100
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <009101c872cf$800fb950$802f2bf0$@nl>
> 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.

        I don't find this obvious. Right-hand side of the assignment should be
evaluated first, so:
i=i+1
i=i

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

                FB

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

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