Re: for loop i++ or ++i

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Michael C# (xyz_at_yomomma.com)
Date: 03/24/05


Date: Thu, 24 Mar 2005 14:45:26 -0500

Think of your for loop in these terms. It's roughly equivalent to a while
loop and an assignment statement:

int i = 0;
while (i < 9)
{
    ...
    i++;
}

OR

int i = 0;
while (i < 9)
{
   ...
   ++i;
}

As you can see, the increment operator occurs at the end of the while loop.
The effects on placement of ++ is more readily apparent in this example:

int i = 0;
// Assigns the value of i to j
// THEN increments i
int j = i++;
Console.WriteLine(j);

i = 0;
// Increments i, then assigns the
// value if i to j
j = ++i;
Console.WriteLine(j);

Thanks.

"Brian" <fake@email.com> wrote in message
news:4021632472603527812500@news.microsoft.com...
> It's a minor thing, but I'm confused on the increment operator in a for
> loop.
>
> I think most usually write:
>
> for (int i = 0; i < something; i++)
> {
> ...
> }
>
> But, in my book (from MSPress) it shows:
>
> for (int i; i < something; ++i)
> {
> ...
> }
>
>
> From what I remember in my readings, the ++i is supposed to be faster,
> performance wise.
>
> As far as results, they both seem to do the same thing in a loop.
> In my tests, of "for (int i = 0, i < 9, ++i OR i++)", both loops iterated
> from 0 to 9.
>
> But, in reading up on the increment operator on MSDN:
>
> ++i = The result of the operation is the value of the operand after it has
> been incremented.
>
> i++ = The result of the operation is the value of the operand before it
> has been incremented.
>
> This doesn't seem to make sense. If ++i returns the value after it has
> been incremented, then if i was initilized to 0, should the first loop in
> the for loop have i = 1????
>
> --Brian
>
>
>
>



Relevant Pages

  • Re: Definition of expression and statement.
    ... The fact that a while loop is recognized ... behavior, e.g., if a is an "int" variable and is initially set to ... The various modifier operators, ... increment and decrement, have TWO uses: ...
    (comp.lang.c)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... it's a for loop in the C sense. ... >> sloppy thinking that results from confusing a programming language ... > The line count increment is necessary because the value is used as an offset ... I really hope you know that the C compiler will generate code to ...
    (comp.programming)
  • Re: Definition of expression and statement.
    ... they end with a semicolon, are not recognized as an "expression" ... The fact that a while loop is recognized ... The various modifier operators, ... increment and decrement, have TWO uses: ...
    (comp.lang.c)
  • Re: The inaugural VB6 vs dot net test
    ... increment a third variable inside the inner loop. ... But I was repsonding to Ulrich's statement about counting down to zero as opposed to counting up to a specific value in order to save a couple of clock cycles. ... WTF is the point of doing that if you are then going to include and additional otherwise unnecessary pointer that gets incremented within the loop, which will eat up as least as many clock cycles as you saved, thereby negating your efforts! ...
    (microsoft.public.vb.general.discussion)
  • Re: bash script example for renaming files (Was: Re: Backing up suggestions?)
    ... it was and it wasn't broke. ... It creates never-ending loop which the line: ... every time it enters a new directory, instead of the continual increment ... Any scripting I'm not doing qualifies. ...
    (alt.os.linux.suse)