i++ and ++i

This is a discussion on i++ and ++i within the C++ Programming forums, part of the General Programming Boards category; plz, guys~ who could tell the difference between something like i++ and ++i ? I'm at a loss now....

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Arrow i++ and ++i

    plz, guys~

    who could tell the difference between something like i++ and ++i ? I'm at a loss now.
    Last edited by black; 05-27-2002 at 07:17 PM.
    Never end on learning~

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    They are just different ways to increment a variable. ++i is a pre increment, i++ is a post increment. Perhaps an example will clear it up.
    Code:
    int main( void )
    {
         int x = 0;
    
         // This will output 0 , then increment variable
         cout << "Post Increment: " << x++ << endl;
    
         // Reset the value
         x = 0;
    
         // This will output 1 because it increment first
         cout << "Pre Increment: " << ++x << endl;
    
         return ( 0 );
    }

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    when using the ++ operator, putting it before the name of variable means that the variable is incremented before it's used, and putting it after means that the variable is incremented after it's used.

    If you are simply incrementing the variable by itself like this:
    Code:
    i++;
    then it doesn't matter if you use ++i or i++

    here are some examples of how it's used:
    Code:
    int i = 10;
    
    int j = i++; // j now is equal to 10, and then i is incremented to 11
    
    int k = ++i; // i is incremented to 12 first, then the value is stored in K
    watch out for this when using for loops! especially if you are using STL linked lists in C++.

    hope this helps!
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    To addition to the stuff the other guys said:

    When optimising, use ++i when you can. why? simple really, i++ is stored in an temporary variable, ++i is not

    for(i = 0; i < 10; ++i)
    (increase i, then use it)


    is faster than

    for(i = 0; i < 10; i++)
    store i in temp, increase temp, use i and then set i = temp. (might be store i in temp, use temp and increase i, or somethin like that)

    But.... Most modern compilers will optimize this for you anyway... but you want to look like an cool optimize-code guy right? I sure want to... Just make sure you don't do logic errors because of this...

    /dave

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    When is i incremented? Before the next statement, or before the next occurence?

    Example
    Code:
    int i = 2;
    
    int p = i++ + i;
    int q = i++;
    q += i;
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    why don't you try it?
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  7. #7
    Nerd Xmevs's Avatar
    Join Date
    Apr 2002
    Posts
    146
    Originally posted by Sang-drax
    When is i incremented? Before the next statement, or before the next occurence?

    Example
    Code:
    int i = 2;
    
    int p = i++ + i;
    int q = i++;
    q += i;
    PHP Code:
    int i 2// i = 2

    int p i++ + i// p = 5 (2 + 3) 
    int q i++; // q = 3
    += i// q = 7 

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Read this thread

    B.t.w. Xmevs, on my system:
    Code:
    int p = i++ + i; // p = 4 (2 + 2)

  9. #9
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Uraldor
    when using the ++ operator, putting it before the name of variable means that the variable is incremented before it's used, and putting it after means that the variable is incremented after it's used.

    If you are simply incrementing the variable by itself like this:
    Code:
    i++;
    then it doesn't matter if you use ++i or i++

    here are some examples of how it's used:
    Code:
    int i = 10;
    
    int j = i++; // j now is equal to 10, and then i is incremented to 11
    
    int k = ++i; // i is incremented to 12 first, then the value is stored in K
    watch out for this when using for loops! especially if you are using STL linked lists in C++.

    hope this helps!
    U.
    Mmm... maybe I should read more.
    Never end on learning~

  10. #10
    Nerd Xmevs's Avatar
    Join Date
    Apr 2002
    Posts
    146
    Originally posted by Monster
    Read this thread

    B.t.w. Xmevs, on my system:
    Code:
    int p = i++ + i; // p = 4 (2 + 2)
    really? so it finishes the entire expression before it increments, then? hmm...

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    > so it finishes the entire expression before it increments
    No it doesn't mean that at all. Don't try and guess from what one particular compiler appears to do.

    http://www.eskimo.com/~scs/C-faq/q3.2.html

    Given the sub-expression
    i++

    i will be incremented at some point "after" the sub-expression has been evaluated, and before the next sequence point. All sorts of things can happen in the mean time, and the compiler is free to choose any convenient moment at which to actually increment i, after it has evaluated the sub-expression i++

    So when you introduce another sub-expression
    i++ + i

    You're really out of luck, since there's nothing say whether it's the left or right sub-expression which gets evaluated first. Even if the left is evaluated first, nothing says that the increment has to happen before the right is evaluated. Remember it only has to happen before the next sequence point.

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