Thread: 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,336
    ++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,318
    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,229
    eg.
    Code:
    int i = 10;
    cout << i-- << endl; //prints 10 (returned THEN decremented)
    cout << --i << endl; //prints 8 (decremented THEN returned)

  5. #5
    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
    28,413
    Actually, it will print 8, because just before that point i=9.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Ireland
    Posts
    8,446
    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.
    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