Initializing loop control variable

Forgetting to Initialize (or re-initialize) the Loop Control Variable. If we depend on a loop control variable starting at zero, then we had better remember to set that value to zero in the first place. And if our loop is itself inside a loop, we must remember to reset the variable so that the second and subsequent loops work correctly. In the following loop:

set the count to zero
while (the count is less than the total number of items)
calculate the result of the next item
add one to the count
end while

forgetting to set the count to zero may well cause trouble.

Can someone explain why this may cause trouble?
Do you have a more concrete example? I'm not sure of any context where this could cause any trouble, since loop control variables are scoped to the loop and nowhere else.
Haha, I wish so myself. That was the only thing he provided for us. If it's this unclear even to others, I suppose the best thing to do would be to ask him again.

He's just not as available as everyone else here which is why I ask so many questions here. No biggie.
closed account (48T7M4Gy)
All you have to do is ask yourself, given the problem, what value should it be set to if it's not zero.

Or you could ask yourself the question if it is initialised at -237 will I get a sensible answer.
Topic archived. No new replies allowed.