Re: Funny loop with post-increment.
Shawn Wildermuth <[email protected]> Tue, 19 Feb 2008 03:39:04 -0500
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <[email protected]> |
Because:
for (int x = 0; x < 10; ++x) {}
Is sooooo pretty?
Thanks,
Shawn Wildermuth
http://adoguy.com
http://wildermuthconsulting.com
Microsoft MVP (C#), MCSD.NET, Author and Speaker
The Silverlight Tour is coming to a city near you!
-----Original Message-----
From: Discussion of development on the .NET platform using any managed
language [mailto:[email protected]] On Behalf Of Ryan Heath
Sent: Tuesday, February 19, 2008 3:36 AM
To: [email protected]
Subject: Re: [DOTNET-CLR] Funny loop with post-increment.
For one reason I had always thought C# had abandoned this peculiar C++
feature...
Anyone know why C# has this feature, its obvious that still people are
confused of it.
Wasn't C# about getting things *simpler and easier* than its cousins C/C++ ?
// Ryan
On Feb 19, 2008 9:24 AM, Jon Skeet <[email protected]> wrote:
> 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(R) http://www.develop.com
>
> View archives and manage your subscription(s) at
http://discuss.develop.com
>
===================================
This list is hosted by DevelopMentorR http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com