1. ## i++ and ++i

How will you best differentiate the two?

i++; //and
++i;

2. ++i increments i and then uses the new value, while i++ uses the old value and then increments i.

3. Originally Posted by alyeska
How will you best differentiate the two?
By looking at them.

Perhaps you would like to reword your question?

4. eg.
Code:
```int i = 10;
cout << i-- << endl; //prints 10 (returned THEN decremented)
cout << --i << endl; //prints 8 (decremented THEN returned)```

5. Code:
```int i = 0;

//first example, b will be 1
int b = ++i;

//second example, b will be 0
int b = i++;```

6. 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

7. Originally Posted by cyberfish
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. Actually, it will print 8, because just before that point i=9.

9. 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.

10. Originally Posted by laserlight
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