# i++ and ++i

• 05-27-2002
black
i++ and ++i
plz, guys~

who could tell the difference between something like i++ and ++i ? I'm at a loss now.:(
• 05-27-2002
MrWizard
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 ); }```
• 05-27-2002
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.
• 05-28-2002
ninebit
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
• 05-28-2002
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;```
• 05-28-2002
Mario
why don't you try it? ;)
• 05-28-2002
Xmevs
Quote:

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 q += i; // q = 7  ```
• 05-28-2002
Monster

B.t.w. Xmevs, on my system:
Code:

`int p = i++ + i; // p = 4 (2 + 2)`
• 05-28-2002
black
Quote:

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...:rolleyes: maybe I should read more. :)
• 05-29-2002
Xmevs
Quote:

Originally posted by Monster

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...
• 05-29-2002
Salem
> 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.