CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    425

    For Loop- Post increment, pre increment count

    Code:
    for (int i = 0; i<=12; i++)
    		cout << i << endl;
    The code above and below gives the same output.
    0
    1
    2
    .
    .
    12
    But i think it should start output from 1 for the code below.
    Code:
    for (int i = 0; i<=12; ++i)
    		cout << i << endl;

    Regards,
    Ankush Mehta

    "The Child is the father of the Man."
    William Wordsworth

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: For Loop- Post increment, pre increment count

    But i think it should start output from 1 for the code below.
    The for statement :
    Code:
    for ( init-expression ; cond-expression ; loop-expression )
       statement
    First the init-expression is executed once.The the cond-expression is evaluated before it continues to the statement. At the end the loop-expression is executed.

    - petter

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: For Loop- Post increment, pre increment count

    The code is equivalent to:

    Code:
    int i=0;
    while ( i <= 12 )
    {
       cout << i << endl;
       ++i;
    }
    Makes no difference if you do i++ or ++i. The latter is preferable as it is sometimes more efficient (eg looping with iterators that are not pointers) and never less efficient.

  4. #4
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    425

    Re: For Loop- Post increment, pre increment count

    okay, fine but then it creates confusion about the concept of post increment & preincrement operator in general. When exactly does the value of post increment is updated if not immediately like pre increment operator (in general, not specifically here).This is quite mysterious for me.

    Regards,
    Ankush Mehta

    "The Child is the father of the Man."
    William Wordsworth

  5. #5
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: For Loop- Post increment, pre increment count

    When exactly does the value of post increment is updated if not immediately like pre increment operator (in general, not specifically here).
    Basically every expression evaluates to a value:
    Code:
    // this evaluates to the value of i
    i; 
    
    // this also evaluates the original value of i (but at the same time i is incremented by one)
    i++;
    
    // this evaluates to the new value of i (after i is incremented by one)
    ++i;
    Code:
    // i is assigned to j before i is incremented
    j = i++;
    
    // i is first incremented, and then assigned to j
    j = ++i;
    - petter

  6. #6
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: For Loop- Post increment, pre increment count

    You can see the difference best if you see the operators as a function:
    Code:
    int PreIncrement(int a)
    {
      a = a + 1;
      return a;
    }
    
    int PostIncrement(int a)
    {
      int temp = a;    // This is the reason why it is slightly slower
      a = a + 1;
      return temp;
    }
    Please don't forget to rate users who helped you!

  7. #7
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    425

    Re: For Loop- Post increment, pre increment count

    then for post increment, 13 should be printed...
    First time, 0 is printed after intialisation and conditional comparison.
    Then i++(i=i+1) evaluates to 0, compares with <=12, Prints 1(cout statement)
    i++ evaluates to 1, compares with <=12, Prints 2
    i++ evaluates to 2, compares with <=12, Prints 3
    i++ evaluates to 3, compares with <=12, Prints 4
    i++ evaluates to 4, compares with <=12, Prints 5
    i++ evaluates to 5, compares with <=12, Prints 6
    i++ evaluates to 6, compares with <=12, Prints 7
    i++ evaluates to 7, compares with <=12, Prints 8
    i++ evaluates to 8, compares with <=12, Prints 9
    i++ evaluates to 9, compares with <=12, Prints 10
    i++ evaluates to 10, compares with <=12, Prints 11
    i++ evaluates to 11, compares with <=12, Prints 12
    i++ evaluates to 12, compares with <=12, Prints 13(//Why this doesn't happen)

    Regards,
    Ankush Mehta

    "The Child is the father of the Man."
    William Wordsworth

  8. #8
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: For Loop- Post increment, pre increment count

    then for post increment, 13 should be printed...
    First time, 0 is printed after intialisation and conditional comparison.
    Then i++(i=i+1) evaluates to 0, compares with <=12, Prints 1(cout statement)
    i++ evaluates to 12, compares with <=12, Prints 13(//Why this doesn't happen)
    The expression 'i++' (by itself) evaluates to 0 (and increase i to 1), but the cond-expression is a differenct expression and sees i as 1 (not 0).

    - petter

  9. #9
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    425

    Re: For Loop- Post increment, pre increment count

    That means for i++, if we cout we get original value but when we use it in an expression the updated or incremented value is used.Am i right if i use this generalisation?

    Regards,
    Ankush Mehta

    "The Child is the father of the Man."
    William Wordsworth

  10. #10
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: For Loop- Post increment, pre increment count

    That means for i++, if we cout we get original value but when we use it in an expression the updated or incremented value is used.Am i right if i use this generalisation?
    No. You'll always 'get' the original value when using i++. With ++i you 'get' the updated value.

    Code:
    // in this example i becomes 7 and j becomes 6 + 5 = 11
    int i = 6;
    int j = (i++) + 5;
    
    // in this example i becomes 7 and j becomes (6+1) + 5 = 12
    int i = 6;
    int j = (++i) + 5;
    - petter

  11. #11
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: For Loop- Post increment, pre increment count

    Some very good explanations here. They should help you out quite a bit.
    Just my two cents here, the way I was taught when I learned C++.

    When used by itself (i.e. not in an expression) it does not not matter
    whether you use post or pre incrementation

    I.e.
    Code:
    for ( int i=0; i <=12; i++)
    Or..
    Code:
    for ( int i=0; i <=12; ++i )
    Now pre-incrementation does reduce CPU cycles because it uses one
    less instruction than post incrementation, but this will make little to no
    difference in most programs that you write. At least while in the learning phase

  12. #12
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: For Loop- Post increment, pre increment count

    Quote Originally Posted by dcjr84
    Now pre-incrementation does reduce CPU cycles because it uses one
    less instruction than post incrementation, but this will make little to no
    difference in most programs that you write. At least while in the learning phase
    One less instruction?
    Not for builtin types.
    For builtin types, there is no difference, as long as the yielded value is not used (at least for all common compiler, including Turbo C++ 1.0).

    And, if the yielded value is used, such as in:
    *p++ vs *++p, then the post-increment is likely to be a bit faster (not significantly) on modern CPU.

    Because, instead of:
    Code:
    ; pre-increment with space optimization
    inc esi
    mov eax,[esi] ; AGI stall (on pentium CPU) + dependency
    
    ; pre-increment with speed optimization
    mov eax,[esi+1] ; four bytes of code are lost, but 1 or 2 CPU cycles gained.
    inc esi
    
    ; post-increment with speed or space optimization
    mov eax,[esi]
    inc esi
    Of course, that is more subtle than that, if other operations are combined, but *p++ is easier to optimize than *++p.

    However, for iterators, ++p is very often MUCH faster than p++.

    So, the rule is : Use ++p as often as you can, and p++ only if you need it (that is, almost never).

    An example of judicious use of it++ is the removal of an item in a list (not an STL std::list) or similar container:
    Code:
    lst.erase(it++); // exception safe : Atomicity, Consistency and Isolation, even if iterator assignments can throw.
    // However, it=lst.erase(it), is fine with STL containers since iterators copy-assignment operators can't throw.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  13. #13
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: For Loop- Post increment, pre increment count

    Thanks for the correction SuperKoko, I appreciate the post.
    I was only trying to say that in general, it is really not going to make a noticeble difference in performance whether you use ++i or i++. Unless you have some specific reason for speed/efficiency optimization.

  14. #14
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    425

    Re: For Loop- Post increment, pre increment count

    Thanks all for enlightening explanations. Just a final confirmation frm ur side:

    Code:
    i=0;
    i++;
    cout << i;
    output will be 1.

    Code:
    i=0;
    ++i;
    cout << i;
    output will be 1.

    is it okay..

    Regards,
    Ankush Mehta

    "The Child is the father of the Man."
    William Wordsworth

  15. #15
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: For Loop- Post increment, pre increment count

    Quote Originally Posted by erankushmehta
    Thanks all for enlightening explanations. Just a final confirmation frm ur side:

    Code:
    i=0;
    i++;
    cout << i;
    output will be 1.

    Code:
    i=0;
    ++i;
    cout << i;
    output will be 1.

    is it okay..
    Yes!
    That's okay!
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured