why i++ instead of ++i in for loops

Discussion in 'C++' started by newtothis, Oct 28, 2003.

  1. newtothis

    newtothis Guest

    I have been reading various texts in C/ C++ and Java. The for lops all
    run along the lines of :



    int i ;

    for(i = 0 ; i < 4 ; i++)

    {

    .....

    }



    I understand the difference between ++i and i++, but I can not see why
    i++ is used in these loops when, as I understand it, the steping
    expression would be more alined to i = i + 1 in this type of case. The
    logic of i++ is not the same. The final result might but the evaluation
    process is not.



    Ok I realise at the end of the loop the value of i is the same which
    ever method you use. But that does not explaine why the preference.



    Also from what I can see at the assembly code level



    int i ;

    for(i = 0 ; i < 4 ; ++i)

    {

    .....

    }



    and



    int i ;

    for(i = 0 ; i < 4 ; i++)

    {

    .....

    }



    are exactly the same.



    Could some please explain the need to use i++ over ++i.


    --
    Posted via http://dbforums.com
    newtothis, Oct 28, 2003
    #1
    1. Advertising

  2. * snip *

    > Could some please explain the need to use i++ over ++i.


    Strictly speaking there is no real need for i++, since "int x = i;
    ++i;" yields the same result as "int x = i++;". As you have noticed in
    for loops with int as loop variable using ++i or i++ doesn't make a
    difference with most compilers. Note that with iterators the ++i version
    may very well be more efficient than i++.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Oct 28, 2003
    #2
    1. Advertising

  3. newtothis

    Artie Gold Guest

    newtothis wrote:
    > I have been reading various texts in C/ C++ and Java. The for lops all
    > run along the lines of :
    >
    > int i ;
    >
    > for(i = 0 ; i < 4 ; i++)
    >
    > {
    > ....
    > }
    >
    > I understand the difference between ++i and i++, but I can not see why
    > i++ is used in these loops when, as I understand it, the steping
    > expression would be more alined to i = i + 1 in this type of case. The
    > logic of i++ is not the same. The final result might but the evaluation
    > process is not.


    Good call, "newtothis"!

    >
    > Ok I realise at the end of the loop the value of i is the same which
    > ever method you use. But that does not explaine why the preference.
    >
    > Also from what I can see at the assembly code level
    >
    > int i ;
    >
    > for(i = 0 ; i < 4 ; ++i)
    > {
    > ....
    > }
    >
    > and
    >
    > int i ;
    >
    > for(i = 0 ; i < 4 ; i++)
    > {
    > ....
    > }
    >
    > are exactly the same.
    >

    Which, by itself is irrelevant -- and only because a compiler can see
    that the return value of `i++', i.e. the original value of `i' is
    thrown
    away.
    >
    > Could some please explain the need to use i++ over ++i.
    >

    Sorry. Can't be done. ;-) There is no `need'.
    What there *is* is `force of habit'. Historical artifact. Custom. Idiom.

    While it is true that there is likely to be no difference in the
    generated code when dealing with basic types, there often *is* a
    difference -- and a potentially significant one -- when dealing with
    user defined types (where `++', prefix and postfix are overloaded
    operators).

    Again, good call. Using the prefix form in such loops is to be
    preferred -- it's just a hard habit to break!

    HTH,
    --ag

    >
    > --
    > Posted via http://dbforums.com




    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.
    Artie Gold, Oct 28, 2003
    #3
  4. > I have been reading various texts in C/ C++ and Java. The for lops all
    > run along the lines of :


    > int i ;


    > for(i = 0 ; i < 4 ; i++)


    > {


    > ....


    > }


    > I understand the difference between ++i and i++, but I can not see why
    > i++ is used in these loops when, as I understand it, the steping
    > expression would be more alined to i = i + 1 in this type of case. The
    > logic of i++ is not the same. The final result might but the evaluation
    > process is not.


    > Ok I realise at the end of the loop the value of i is the same which
    > ever method you use. But that does not explaine why the preference.


    You have hit on one of the reasons that "Accelerated C++" always uses the
    "++i" form unless something is going to be done with the value of the
    expression. So, for example, we would write

    ++i;

    instead of

    i++;

    but we would write

    n = a[i++];

    if it were appropriate to do so--the point being that the value of "i++" is
    used in the second example.

    Incidentally, instead of

    for (i = 0; i < 4; i++)

    we would write

    for (i = 0; i != 4; ++i)

    because that way, we can use the same general form of loop for all kinds of
    iterators in addition to integers.
    Andrew Koenig, Oct 28, 2003
    #4
  5. newtothis

    jeffc Guest

    "newtothis" <> wrote in message
    news:...
    >
    > I understand the difference between ++i and i++, but I can not see why
    > i++ is used in these loops when, as I understand it, the steping
    > expression would be more alined to i = i + 1 in this type of case. The
    > logic of i++ is not the same. The final result might but the evaluation
    > process is not.
    > Ok I realise at the end of the loop the value of i is the same which
    > ever method you use. But that does not explaine why the preference.


    I think you've answered your own question. In that case, the result is the
    same so it really doesn't matter. If it doesn't matter, i++ seems more
    aesthetically pleasing to most people, since logically you see the variable
    first, and then you see what to do with it. If these were classes with
    member functions, it would look like this.

    Int i(1);
    i.increment();
    vs.
    increment().i;

    The first looks more natural.
    jeffc, Oct 28, 2003
    #5
  6. newtothis

    Howard Guest

    "Andrew Koenig" <> wrote in message
    news:3xvnb.197400$...

    > ...So, for example, we would write
    >
    > ++i;
    >
    > instead of
    >
    > i++;
    >
    > but we would write
    >
    > n = a[i++];
    >
    > if it were appropriate to do so--the point being that the value of "i++"

    is
    > used in the second example.
    >


    What do you mean that "the value of "i++" is used in the second example"?
    If you're referring to n = a[i++], then the value that is used to index into
    a is i, not i++. The increment is done afterwards. If, instead, you're
    just saying that you should only use i++ when you need the value of i++,
    again, I don't see how you can get that value, except in a following
    statement, as in

    x = a[i++];
    n = a; // here we use the incremented value of i

    I think I see what you were getting at, but your phrasing is a little
    confusing.

    -Howard
    Howard, Oct 28, 2003
    #6
  7. > > ...So, for example, we would write
    > >
    > > ++i;
    > >
    > > instead of
    > >
    > > i++;
    > >
    > > but we would write
    > >
    > > n = a[i++];
    > >
    > > if it were appropriate to do so--the point being that the value of "i++"
    > > is used in the second example.
    > >

    >
    > What do you mean that "the value of "i++" is used in the second example"?
    > If you're referring to n = a[i++], then the value that is used to index

    into
    > a is i, not i++.


    I meant exactly what I said: The value of i++ is used in the second
    example. Now, as it happens, if i is an integer, then the value of i++ is a
    copy of what the value of i was before i was incremented. If i is a
    user-defined type, the value of i++ might be something different. Either
    way, evaluating the expression a[i++] involves using the value of i++.
    Andrew Koenig, Oct 28, 2003
    #7
  8. newtothis

    Howard Guest

    "Rolf Magnus" <> wrote in message
    news:bnm57f$sbn$07$-online.com...
    > Howard wrote:
    >
    > >
    > > "Andrew Koenig" <> wrote in message
    > > news:3xvnb.197400$...
    > >
    > >> ...So, for example, we would write
    > >>
    > >> ++i;
    > >>
    > >> instead of
    > >>
    > >> i++;
    > >>
    > >> but we would write
    > >>
    > >> n = a[i++];
    > >>
    > >> if it were appropriate to do so--the point being that the value of
    > >> "i++"

    > > is
    > >> used in the second example.
    > >>

    > >
    > > What do you mean that "the value of "i++" is used in the second
    > > example"? If you're referring to n = a[i++], then the value that is
    > > used to index into a is i, not i++.

    >
    > i and i++ have the same value. The value of postfix++ is the value that
    > the operand had before the increment.
    >
    > > The increment is done afterwards.

    >
    > Right.
    >
    > > If, instead, you're just saying that you should only use i++ when you
    > > need the value of i++, again, I don't see how you can get that value,

    >
    > int a = i++;
    >
    > now a has the value of i++.
    >
    > > except in a following statement, as in
    > >
    > > x = a[i++];
    > > n = a; // here we use the incremented value of i
    > >
    > > I think I see what you were getting at, but your phrasing is a little
    > > confusing.

    >
    > You seem to be confused by the term "value of i++". The value of i++ is
    > what you get when you e.g. assign "i++" to something, not the value of
    > i after i++ has been executed.
    >


    That's exactly where I was confused. Which is why I asked what he meant by
    "the value of i++".

    It seems very strange to refer to the "value of i++" unless you were
    refering to the value you get by performing the ++ operation upon the i
    variable, which is obviously not the intent. To me, it would be like
    referring to the "value of 7+1", and intending to refer to the value 7
    (before adding one), whereas in normal conversation, you'd be talking about
    the value 8, which is what 7+1 is equal to. See why it's confusing (at
    least to me)?

    No argument here on the "truth" of what Andrew said...just confusion on the
    terminology.

    -Howard
    Howard, Oct 28, 2003
    #8
  9. newtothis

    Howard Guest

    "Rolf Magnus" <> wrote in message
    news:bnmaat$ir6$06$-online.com...

    > Seems logical to me:
    >


    Oh well. I find it confusing, you don't. We'll both live. :)

    -Howard
    Howard, Oct 28, 2003
    #9
  10. newtothis

    newtothis Guest

    So from what has been said, except for may be the situation of an
    oveloaded operator, the only reason for ++i vs i++ in the for loop is
    programmer preference.

    This then is close to the arguments of underscore vs mixedcase
    vqariables.

    Personal preference.



    Why then dont the writeres of texts explain this instead of trying to
    lay down some form of undefinable law?



    Thanks for these comments as it clears up some programming "rules"
    people have tried to push on me.


    --
    Posted via http://dbforums.com
    newtothis, Oct 28, 2003
    #10
  11. newtothis

    Duane Hebert Guest


    > I think you've answered your own question. In that case, the result is

    the
    > same so it really doesn't matter. If it doesn't matter, i++ seems more
    > aesthetically pleasing to most people, since logically you see the

    variable
    > first, and then you see what to do with it. If these were classes with
    > member functions, it would look like this.


    Actually that's not true. i++ has to create a temporary and ++i doesn't.
    This may
    not be a big deal when i is an int, but it gets pretty inefficient when i is
    a larger type or
    an iterator etc.
    Duane Hebert, Oct 29, 2003
    #11
  12. > So from what has been said, except for may be the situation of an
    > oveloaded operator, the only reason for ++i vs i++ in the for loop is
    > programmer preference.


    Not quite. There's no difference in practice if i is an integer variable,
    but if it's an iterator, there may be a performance difference. There is
    also the philosophical question of whether it's a good thing to ask the
    machine to do work that you know is going to be discarded.

    > Why then dont the writeres of texts explain this instead of trying to
    > lay down some form of undefinable law?


    Some of them do.
    Andrew Koenig, Oct 29, 2003
    #12
  13. newtothis <> wrote in message news:<>...
    > So from what has been said, except for may be the situation of an
    > oveloaded operator, the only reason for ++i vs i++ in the for loop is
    > programmer preference.


    Yes.

    >
    > This then is close to the arguments of underscore vs mixedcase
    > vqariables.


    Not at all. In situations as the one you mentioned, you usually
    acquire a habit of either writing i++ or ++i. If you write ++i you are
    assured of optimal performance, if you write i++, you're not. Thus you
    should always prefer the ++i version.
    >
    > Personal preference.
    >
    >
    >
    > Why then dont the writeres of texts explain this instead of trying to
    > lay down some form of undefinable law?
    >

    I do believe that most good textbooks will tell you that you should
    prefer ++i. At least they ought to do so.
    >
    >
    > Thanks for these comments as it clears up some programming "rules"
    > people have tried to push on me.


    Kind regards
    Peter Koch Larsen
    Peter Koch Larsen, Oct 29, 2003
    #13
  14. newtothis

    Rolf Magnus Guest

    newtothis wrote:

    >
    > I have been reading various texts in C/ C++ and Java. The for lops all
    > run along the lines of :
    >
    >
    >
    > int i ;
    >
    > for(i = 0 ; i < 4 ; i++)
    >
    > {
    >
    > ....
    >
    > }
    >
    >
    >
    > I understand the difference between ++i and i++, but I can not see why
    > i++ is used in these loops when, as I understand it, the steping
    > expression would be more alined to i = i + 1 in this type of case. The
    > logic of i++ is not the same. The final result might but the
    > evaluation process is not.


    For builtin types, it really doesn't matter. But in C++, you can write
    and operator++ for your own class. And then it might matter, becaure
    postfix ++ has to create a copy of the object so that the old value can
    be returned. If you don't need the return value, that copy is
    unnecessary. If the compiler doesn't do named return value
    optimization, that copy might even need to be copied again, and all
    that just to throw the result away. The postfix operator++ for an own
    class might look something like this:

    MyClass MyClass::eek:perator++(int)
    {
    MyClass retval(*this); // copy the object
    // do whatever is needed to "increment" the object
    reutrn retval; // return the copy by value
    }

    while prefix ++ might look like:

    MyClass& MyClass::eek:perator++()
    {
    // do whatever is needed to "increment" the object
    return *this; // return a refernce to the object
    }

    Therefore, it's considered a good habit to always use prefix ++ if the
    return value is not needed.
    Rolf Magnus, Oct 29, 2003
    #14
  15. newtothis

    newtothis Guest

    Thanks for all the comments. I did not intend to cause a general stir.
    This though has confirmed that my use of ++i, when appropiate, in such
    loops has been correct.



    I realise at times the use of i++ is appropiate and is dependent on the
    intent of the design and code.



    I now just have to get over the issues of mixed case or underscores in
    variables. That seems to have been discussed at some length anyway.



    Thanks again



    Newtothis


    --
    Posted via http://dbforums.com
    newtothis, Oct 29, 2003
    #15
  16. newtothis

    Rolf Magnus Guest

    Howard wrote:

    >
    > "Andrew Koenig" <> wrote in message
    > news:3xvnb.197400$...
    >
    >> ...So, for example, we would write
    >>
    >> ++i;
    >>
    >> instead of
    >>
    >> i++;
    >>
    >> but we would write
    >>
    >> n = a[i++];
    >>
    >> if it were appropriate to do so--the point being that the value of
    >> "i++"

    > is
    >> used in the second example.
    >>

    >
    > What do you mean that "the value of "i++" is used in the second
    > example"? If you're referring to n = a[i++], then the value that is
    > used to index into a is i, not i++.


    i and i++ have the same value. The value of postfix++ is the value that
    the operand had before the increment.

    > The increment is done afterwards.


    Right.

    > If, instead, you're just saying that you should only use i++ when you
    > need the value of i++, again, I don't see how you can get that value,


    int a = i++;

    now a has the value of i++.

    > except in a following statement, as in
    >
    > x = a[i++];
    > n = a; // here we use the incremented value of i
    >
    > I think I see what you were getting at, but your phrasing is a little
    > confusing.


    You seem to be confused by the term "value of i++". The value of i++ is
    what you get when you e.g. assign "i++" to something, not the value of
    i after i++ has been executed.
    Rolf Magnus, Oct 29, 2003
    #16
  17. newtothis

    Rolf Magnus Guest

    Howard wrote:

    >
    > "Rolf Magnus" <> wrote in message
    > news:bnm57f$sbn$07$-online.com...
    >> Howard wrote:
    >>
    >> >
    >> > "Andrew Koenig" <> wrote in message
    >> > news:3xvnb.19740

    $...
    >> >
    >> >> ...So, for example, we would write
    >> >>
    >> >> ++i;
    >> >>
    >> >> instead of
    >> >>
    >> >> i++;
    >> >>
    >> >> but we would write
    >> >>
    >> >> n = a[i++];
    >> >>
    >> >> if it were appropriate to do so--the point being that the value of
    >> >> "i++"
    >> > is
    >> >> used in the second example.
    >> >>
    >> >
    >> > What do you mean that "the value of "i++" is used in the second
    >> > example"? If you're referring to n = a[i++], then the value that is
    >> > used to index into a is i, not i++.

    >>
    >> i and i++ have the same value. The value of postfix++ is the value
    >> that the operand had before the increment.
    >>
    >> > The increment is done afterwards.

    >>
    >> Right.
    >>
    >> > If, instead, you're just saying that you should only use i++ when
    >> > you need the value of i++, again, I don't see how you can get that
    >> > value,

    >>
    >> int a = i++;
    >>
    >> now a has the value of i++.
    >>
    >> > except in a following statement, as in
    >> >
    >> > x = a[i++];
    >> > n = a; // here we use the incremented value of i
    >> >
    >> > I think I see what you were getting at, but your phrasing is a
    >> > little confusing.

    >>
    >> You seem to be confused by the term "value of i++". The value of i++
    >> is what you get when you e.g. assign "i++" to something, not the
    >> value of i after i++ has been executed.
    >>

    >
    > That's exactly where I was confused. Which is why I asked what he
    > meant by "the value of i++".
    >
    > It seems very strange to refer to the "value of i++" unless you were
    > refering to the value you get by performing the ++ operation upon the
    > i
    > variable, which is obviously not the intent. To me, it would be like
    > referring to the "value of 7+1", and intending to refer to the value 7
    > (before adding one), whereas in normal conversation, you'd be talking
    > about
    > the value 8, which is what 7+1 is equal to. See why it's confusing
    > (at least to me)?


    Seems logical to me:

    int x = 3 + 1;

    the value of 3 + 1 is 4, so x is set to 4.

    int y = x;

    the value of x is 4, so y is set to 4.

    int f(int i)
    {
    return i+1;
    }

    y = f(x);

    the value of f(x) is 5, so y is set to 5 now.

    int g(int& i)
    {
    int retval = i;
    ++i;
    return retval;
    }

    y = g(x);

    the value of g(x) is 4, so y is set to 4.

    z = x++;

    the value of x++ is 5, so z is set to 5 now.
    Rolf Magnus, Oct 29, 2003
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    853
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,739
    Smokey Grindel
    Dec 2, 2006
  3. brad
    Replies:
    1
    Views:
    496
    Waldemar Osuch
    Aug 5, 2008
  4. Me
    Replies:
    2
    Views:
    229
  5. Aaron
    Replies:
    2
    Views:
    507
    dhtml
    Apr 10, 2011
Loading...

Share This Page