Prefer pre-increment and pre-decrement

This tip submitted by Karl on 2006-01-04 01:51:13. It has been viewed 26843 times.
Rating of 5.5 with 174 votes



As far as possible one should use ++i and --i rather than i++ or i--. In a nutshell one should always use the pre increment/decrement forms rather than the post ones.The reason being, post increment retains the old value (usually in a temporary variable) and usually one does not end up using the old value, so that temp variable ends up by just wasting instructions.

Thus always use the pre-increment/decrement statements rather than the post ones. This is most important for complex types like iterators. It tends to be mostly irrelevant for integers.



More tips

Help your fellow programmers! Add a tip!