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

3

6

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!

Install answered 8/2, 2015 at 10:10 Comment(3)
In short: No, there isn't any difference for the number of iterations.Envious
See for loop.Ibbie
At that place of the loop, they are equivalent. Also note that compilers will almost certainly exchange the post-increment with a pre-increment at that place, at least when optimizations are turned on.Wrinkly
V
18

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;
    }
}
Varicose answered 8/2, 2015 at 10:16 Comment(3)
You can look here for more examples: en.cppreference.com/w/cpp/language/forHanks
@Varicose Cheers for the help!Install
A mention that in devug builds, ++ pre will be way faster: and poinlessly slow debug buikds can be annoying to work with.Airline
P
5

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?).

Porcupine answered 8/2, 2015 at 10:21 Comment(2)
Looks like an infinite loop to me.Ibbie
x here would be replaced by ++counter or counter++. I'm following the convention suggested by the poster.Porcupine
R
1

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)?

Riyal answered 8/2, 2015 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.