Does for loop iteration (3rd statement) make a difference if it is ++i over i++? | SoloLearn: Learn to code for FREE!

+1

Does for loop iteration (3rd statement) make a difference if it is ++i over i++?

Im puzzled as to why the ++i doesnt skip the first statement in a for loop because it is supposed to change i=0 to i=1 before even using i itself. And to top it off when I use both ++i and i++ against each other I dont see a difference. Can someone explain why this is happening?

10/19/2018 3:55:02 PM

Evan Martine

2 Answers

New Answer

+3

The order a for loop is executed is: initialization -> comparison, if true -> body -> increment -> repeat comparison comparison, if false -> end The increment is standalone, it's exactly the same as if you put ++i; or i++; on its own line somewhere else in the code. The result of i is the same either way.

+1

Dennis Thanks for explaining the order of operations in a for loop