Why does it tend to get into an infinite loop if I use continue
in a while
loop, but works fine in a for
loop?
The loop-counter increment i++
gets ignored in while
loop if I use it after continue
, but it works if it is in for
loop.
If continue
ignores subsequent statements, then why doesn't it ignore the third statement of the for
loop then, which contains the counter increment i++
? Isn't the third statement of for
loop subsequent to continue
as well and should be ignored, given the third statement of for
loop is executed after the loop body?
while(i<10) //causes infinite loop
{
...
continue
i++
...
}
for(i=0;i<10;i++) //works fine and exits after 10 iterations
{
...
continue
...
}
Because the third part of the for is always executed.
for loop holds condition statements and increment, so when the condition is satisfied it goes to execute the statement inside for loop,but if write continue statement than it will again reached to first line of for loop i.e. increment and checking of condition statement, if satisfied than again comes in for execution. For while loop it just checks the condition statement and if condition satisfied it goes for the execution of statements in the while loop. so continue will not execute any line after it.and hence your condition satisfied every time and goes for the infinite loop.
Because
continue
goes back to the start of the loop. Withfor
, the post-operationi++
is an integral part of the loop control and is executed before the loop body restarts.With the
while
, thei++
is just another statement in the body of the loop (no different to something likea = b
), skipped if youcontinue
before you reach it.continue
statement jumps the control to the end of the statements in current iteration of loop i.e. it skips the execution of the statements in the current iteration and moves to the next iteration of the loop.With
while
loop,continue
statement causes control to reach the end of statements (including increment statement), thus causing loop to continue forever.With
for
loop,continue
statement jumps the control to end of statement and excutes the increment statement (Infor
loop, increment statement is considered seperate from the statments written within the body of the loop).The reason is because the
continue
statement will short-circuit the statements that follow it in the loop body. Since the way you wrote thewhile
loop has the increment statement following thecontinue
statement, it gets short-circuited. You can solve this by changing yourwhile
loop.A lot of text books claim that:
is equivalent to:
But, in reality, it is really like:
Or, to be a little more pedantic:
These are more equivalent, since now if the body of the
while
has acontinue
, the increment still occurs, just like in afor
. The latter alternative only executes the increment after the iteration has completed, just likefor
(the former executes the increment before the iteration, deferring to save it ini
until after the iteration).Your increment of
i
is after continue, so it never gets executed