# CodeGuru Technical FAQs > CodeGuru Individual FAQs >  C++ Operator: Why should I use '++i' instead of 'i++'?

## Yves M

*Q:* Someone told me that if I want to increment a variable _and that's all_, I should use *++x* instead of *x++*.  Is this true?

*A:* If incrementing is all that you want to do (you are not assigning the variable to something else), then yes this is a good practice, _especially if you are using the increment operator on a class_.



```
// Prefix operators are preferred in cases in the following common cases:

for (;checkstate(x);++x) dosomething(x);

++x;
```

For the standard data types there is usually no performance difference, but for classes there are!  The reason is that _(for most common implementations of)_ postfix operators retain a temporary copy of original variable and because the return value is returned by value, not by reference. The following code exemplifies this:



```
class MyClass
{
public:
    MyClass& operator++()    //Prefix increment operator  (++x)
    {
        //Perform increment operation
        return *this;
    }

    MyClass  operator++(int) //Postfix increment operator (x++)
    {
        MyClass temp = *this;
        //Perform increment operation
        return temp;
    }
};
```

FAQ contributed by: [Kevin Hall]

----------


## KevinHall

*Q:* So what form of increment/decrement operator should I use for fundamental types when I can use either.  For example:



```
double d = 3.14;
d--; // or should I use --d?

for (int i=0; i<10; i++) [COLOR=#008800]// or should I use ++i?
```

*A:* Use the pre-incement/decrement form (++i and --i)

Even though there is little impact for fundamental types, since we programmers are human, you will produce programming habits.  And those habits will cary over to when we deal with classes instead, for example iterators.  

There was an informal study done (sorry I forgot the source) on a number of people who say "I only use post-incrementing/decrementing for fundamental types."  The results showed that they very frequently used the post-increment/decrementing operators for iterators and other classes leading to a great amount of wasted CPU cycles in the end code.

----------


## exterminator

There is very little gain over efficiency for fundamental types and efficiency as a concern for them doesn't help you decide which to choose. The underlying idea should be in your mind. The post-increment causes a copy to be made, which becomes useless. Such style should be avoided.

Moreover, there can be situations where you need to use the post-increment rather than pre-increment. For example, when removing an element from a standard map container object.

```
mapObject.erase(aValidIterator++);
```

Of course, you can use a copy explicitly and use pre-increment but this erase functionality looks more elegant as a one-liner.

Also, take a look at the following - Which is faster, i++ or ++i?

----------


## TheCPUWizard

> There is very little gain over efficiency for fundamental types and efficiency as a concern for them doesn't help you decide which to choose.


There should have been ..."in recent years (about 20)"...

The pre-increment was specifically designed for "C" on the DEC PDP-11 (remember "C" has only fundamental types), because there was a specific hardware instruction that performed this operation directly on the mrmory location, without the overhead of a load,add,store sequence. The result was significantly faster.

There are still a few processors that can do this more efficiently (than the post increment of a fundamental type), but processors are often 1000's of times faster today and the situations are extremely rare. The *only* place I can (possibly) recomment the use of a post increment is:



```
int j = i++;  // Save the pre-incremented value for later calculations.
```

This is (at least to me) more readable and maintainable [and is of higher efficiency than:


```
int j = i; ++i;
```

----------

