In Defense of Increment I

Pre-increment vs Post-increment Operator in Loops

When last did you see ++i in a C, C++, C#, Java, or JavaScript program? On the other hand, you would have seen i++ numerous times. Why is this the preferred choice?
PREREQUISITES — You should already…
  • have some knowledge of and programming experience in C, C++, C#, Java or JavaScript.

Operators vs Functions

Operators work very much like functions, in that they:

  1. Are passed arguments - which we call operands.
  2. Perform a task — the job/task that their names generally indicate e.g. addition.
  3. Return a value — the result of the task or calculation (not to be confused with its job/task).

The difference between functions and operators is thus in the syntax and terminology, rather than in the concepts. We use operator notation to call operators. And to be more efficient, since operators are so common in programming languages, the tasks they perform are expanded inline.

Side-effect Operators

Most operators do not affect the state of memory other than their own local variables, and their tem­po­ra­ry return value. The few operators that do affect memory - i.e. change the state of the program before they return - are called side-effect operators. Most operators only require that their operands have a type and a value. But side-effect operators require more: their operands must also represent modifiable memory. Some languages refer to this requirement as lvalues.

The pre- and post-increment operators (and their cousins, the pre- and post-decrement operators,) require lvalues.

A simple example will illustrate this difference. The addition operator (+) is used on operands that have a numeric type and value - it can be used on literal values like 7 and 5, or on variables that re­pre­sent numeric values. The increment and decrement operators, on the other hand, can only be used on variables (i.e. modifiable memory) that represent numeric values. They cannot be used on nu­me­ric literals.

Pre-increment vs Post-increment

Now let's compare the pre-increment and post-increment operators.

The pre-increment operator:

  1. Requires an lvalue operand.
  2. Increments the actual memory of the operand (not a copy).
  3. Returns the new value.

The post-increment operator:

  1. Requires an lvalue argument.
  2. Make a copy of the current value.
  3. Increments the actual memory of the operand (not a copy).
  4. Returns the previous value (the copy saved).

As we can see, the only difference is at steps 3 & 4: they both perform the same job, but they return a dif­fer­ent value. If the return value is used in code, this is significant. But if your code does not use the return value, then it makes no difference which operator you use. In fact, you will find that optimising compilers produce the exact same machine code in both cases.

Unless a compiler optimises your code, chances are that the post-increment code will require more instructions than pre-increment, and consequently be slower.

Just a Loop

So we ask the question: Why do we almost always see i++ instead of ++i, particularly in loops?

Here's the simple answer: it has become a meme, a pattern that works and is passed from pro­gram­mer to pro­gram­mer, with no need to even think about it. That's fine - until you find that many will argue that the following two loops will execute differently:

These two loops will produce identical results (WORK is performed 5 times). Programmers who think they should execute dif­fer­ent­ly, maybe do not completely understand either: the rules of increment operator behaviour, or are vague about the sequence and operation of the for() loop. What about the following set of for() loops?:

Considering that the expressions of the for() loop are optional, we may omit the third. And since the for() loop never uses the result of the third expression, these loops will peform exactly like the first set.

Here is another example with two while loops, which are algorithmically the same as the above for loops (semantically, there is a small difference regarding the scope of i, which we mitigated to some degree with the enclosing compound statement):

Once again, these loops do not execute differently: WORK is peformed 5 times.

Why are the results the same? In all the loops above, the return value of the operators is discarded and is not used. The only effect of the operators is the job they perform, and as we've seen, they per­form the same job.

It's Your Choice

So which operator should you use when you do not care about its result (return value)? You are welcome to continue to use i++, since it has become the de facto pattern since “The C Programming Language” by Kernighan and Ritchie. But as a pro­gram­mer, you need to understand that in for() loops, i++ is just a pattern, not a requirement or special syntax. And you could, on occasion, be a little daring and use that poor neg­lec­ted ++i in other situations where the return value does not matter.

2019-02-28: Fixed type ‘lopp’ for ‘loop’. And clarifications. Thanks Francois van Eden. [brx]
2017-11-19: Update to new admonitions. [brx]
2017-09-21: Edited. [brx;jjc]
2016-03-05: Created. [brx]