i++ and ++i

This is a discussion on i++ and ++i within the C++ Programming forums, part of the General Programming Boards category; How will you best differentiate the two? i++; //and ++i;...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    i++ and ++i

    How will you best differentiate the two?

    i++; //and
    ++i;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    ++i increments i and then uses the new value, while i++ uses the old value and then increments i.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by alyeska View Post
    How will you best differentiate the two?
    By looking at them.

    Perhaps you would like to reword your question?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    eg.
    Code:
    int i = 10;
    cout << i-- << endl; //prints 10 (returned THEN decremented)
    cout << --i << endl; //prints 8 (decremented THEN returned)

  5. #5
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Code:
    int i = 0;
    
    //first example, b will be 1
    int b = ++i;
    
    //second example, b will be 0
    int b = i++;

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Note also that in C++, when you are using --x or ++x for your own class (e.g. an iterator), then the calculation is simpler than for x++ or x--, because the latter requires a temporary copy of the value to be returned, and this adds extra work for the compiler. This has been discussed quite a lot - many C++ programmers therefor prefer to use --x or ++x whenever possible, even on standard base-types that aren't subject to custom operator functions [and thus have no difference in performance].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Emulator
    Join Date
    Feb 2008
    Posts
    43

    Smile

    Quote Originally Posted by cyberfish View Post
    eg.
    Code:
    int i = 10;
    cout << i-- << endl; //prints 10 (returned THEN decremented)
    cout << --i << endl; //prints 8 (decremented THEN returned)
    On the last part, it will print 9. Just because there are two -'s doesn't mean it will decrement by one. Which one to use? Depends on what you're programming.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Actually, it will print 8, because just before that point i=9.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,460
    Uh? For starters you are worng. It prints 8. Next, did you really have a question or are you wasting our time. You seem to know the answer.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Emulator
    Join Date
    Feb 2008
    Posts
    43

    Talking

    Quote Originally Posted by laserlight View Post
    Actually, it will print 8, because just before that point i=9.
    Oops, I saw that statement by itself and the declaration of the variable. So I guess it would print 8. Good catch :P

Popular pages Recent additions subscribe to a feed

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