izaak_pyzaak
izaak_pyzaak

Reputation: 960

In a for loop, is there a difference between pre/post-incrementing a loop control variable in terms of the total quantity of iterations?

When I compile and run the code below with either counter++ or ++counter substituted for x, the output is identical; in both cases, numbers 1 - 10:

for (int counter = 1; counter < 11; x)
{
    std::cout << counter << endl;
}

Originally I thought ++counter would increment by 1 and then return the new value before for boolean expression in the loop header was evaluated. i.e when starting with counter = 1 and using ++counter, counter would have a value of 2 in the boolean expression. This appears to not be the case, as both outputs are identical rather than the ++counter version having one less iteration, like I expected.

Reading around, it appears ++counter and counter++ increment counter by 1 at either the start or end of the loop body respectively. In which case is this not, at least conceptually, an identical action? Because the end and the start of the loop are the same thing once the loop has past the first iteration.

The only time I can see this making a difference is in the first iteration, where std::cout << counter << endl;should output 1 to the console if counter++ is used (because 1 is added to counter at the end of the loop). Whilst std::cout << counter << endl; should output 2 to the console if ++counter is used (because 1 is added to counter at the start of the loop).

In addition to the question above, could you please precisely explain the order in which the three actions are evaluated in the for loop header, and explain exactly where the iterations occur when using i++ and ++i.

Many thanks!

Upvotes: 6

Views: 3802

Answers (3)

Ori Folger
Ori Folger

Reputation: 1086

The C++ for-loop may be roughly considered as syntactic sugar for the following (using your example):

int counter = 1;
while (counter < 11)
{
    std::cout << counter << endl;
    x;
}

So, the initialization statement is executed first, the condition expression is executed before each iteration, and the increment statement is executed at the end of each iteration.

We can see here that using post-increment or pre-increment operators makes no difference to the loop logic, though there may be other differences (post-increment actually requires keeping a copy of the old value of the variable as that is the value of the expression, which has some associated costs, see Preincrement faster than postincrement in C++ - true? If yes, why is it?).

Upvotes: 5

counter++ makes a copy increase counter and returns the value

++counter increases counter and returns counter.

In loop

for(initialization;condition;increment/decrement)

{body;}

increment/decrement is the last line of the loop. So it will start the loop again when increment/decrement returns a value. So post-increment or pre-increment will not affect here. See this

What is the difference between pre-increment and post-increment in the cycle (for/while)?

Upvotes: 1

StenSoft
StenSoft

Reputation: 9617

There is no difference. In older compilers, ++counter was faster because it did not create a temporary variable but all modern compilers can optimize that out. For heavy objects with custom (non-inlined) increment operators, ++counter can still be more efficient.

As for when evaluation takes place:

for (initialization; condition; increment/decrement)
    code;

is evaluated as

{
    initialization;
    while (condition)
    {
        code;
        increment/decrement;
    }
}

Upvotes: 18

Related Questions