++i vs i++ ... what's the difference?

This is a discussion on ++i vs i++ ... what's the difference? within the C++ Programming forums, part of the General Programming Boards category; This might be dumb question, but can someone tell me what is the difference? I read somewhere that when your ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    ++i vs i++ ... what's the difference?

    This might be dumb question, but can someone tell me what is the difference? I read somewhere that when your evaluating something if you use ++i, "i" is increased then function is evaluated, but if you use i++, function is evaluated then "i" is increased.

    But when I ran this:
    Code:
    int n = 0;
    for (int i = 0; i < 10; ++i)
    {
    	n++;
    	std::cout << n << ". " << i << std::endl;
    }
    and this:

    Code:
    int n = 0;
    for (int i = 0; i < 10; i++)
    {
    	++n;
    	std::cout << n << ". " << i << std::endl;
    }

    in both cases I get:
    Code:
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
    6. 5
    7. 6
    8. 7
    9. 8
    10. 9
    So what is the difference?
    Don't know if that makes any difference but this is run on Visual Studio 2005

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by ranko_6 View Post
    This might be dumb question, but can someone tell me what is the difference? I read somewhere that when your evaluating something if you use ++i, "i" is increased then function is evaluated, but if you use i++, function is evaluated then "i" is increased.
    Your understanding is correct.

    But when I ran this:
    in both cases I get:
    Code:
    1. 0
    2. 1
    3. 2
    4. 3
    5. 4
    6. 5
    7. 6
    8. 7
    9. 8
    10. 9
    Since the ++ operators are in their own, self-contained statements, it makes no difference.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The difference between prefix (++x) and postfix (x++) only matters if you actually use the value, like this.
    Code:
    y = array[x++];
    z = ++x;
    As brewbuck said, if it's by itself in a statement unto itself as you have it then there's no difference.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    Thanks for info, tried with "if" statement and it worked.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    The ++ operators also work with some class types (like iterators). In some cases it can be more efficient to use ++i instead of i++ when you aren't using the return value. Because of that, and because almost all other factors are equal, many people prefer to use ++i over i++ when the return value is not used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 05:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Replies: 6
    Last Post: 08-26-2006, 11:09 PM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 04:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21