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

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

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

Initially, I thought ++counter would increment by 1, and then return a new before value to evaluate the boolean expression in the loop header. that is, starting with counter = 1 and using ++counter , counter will have a value of 2 in a boolean expression. This does not seem to be the case, since both outputs are identical, not the ++counter version, which has another iteration, as I expected.

When read, ++counter and counter++ increment counter appear at 1 at the beginning or end of the loop body, respectively. In this case, is it, at least conceptually, an identical action? Because the end and the beginning of the cycle are one and the same as soon as the cycle has passed the first iteration.

The only time I see that it matters is the first iteration, where std::cout << counter << endl; should output 1 to the console if counter++ (because 1 is added to the counter at the end of the loop). While std::cout << counter << endl; should output 2 to the console if ++counter (because 1 is added to the counter at the beginning of the loop).

In addition to the question discussed above, could you please explain the exact 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 .

Thank you very much!

+6
source share
3 answers

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

Regarding the assessment:

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

estimated as

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

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

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

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

We see here that the use of post-increment or pre-increment statements has nothing to do with the logic of the loop, although there may be other differences (post-increment actually requires a copy of the old value of the variable, the value of the expression, which has some this costs, see. Recursing faster than postincrement in C ++ is true? If so, why?. )

+5
source

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

++ counter increments the counter and returns the counter.

In the loop

 for(initialization;condition;increment/decrement) {body;} 

Increment / decrease is the last line of the loop. This way it will start the loop again when increment / decment returns the value. Therefore, the post-increment or pre-increment will not affect here. See This

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

+1
source

Source: https://habr.com/ru/post/982165/


All Articles