continue Statement

Sometimes, in a loop under some condition, one wants to not complete or exit the loop but start the next iteration of the loop - skipping the rest of the statements within the loop (from that point to the end of the loop). One could use a "goto" statement but the possible problems with that have already been discussed. To avoid the "goto", a new statement called the "continue" statement has been invented.

When a "continue" statement is encountered in the control flow of a program, it causes the rest of the statements in loop body to be skipped and the next loop started. What actually happens is slightly different for each of the three kinds of loops.

A "continue" within a "while" statement causes control flow to jump to the test of the condition at the top of the loop. And as usual, if the condition is true to execute the loop, otherwise to terminate the loop and jump to the first statement after the "while" loop.

A "continue" within a "do" ... "while" loop causes control flow to jump to the test of the condition at the end of the loop. And as usual, if the condition is true to repeat the loop, otherwise to terminate the loop and jump to the first statement after the loop.

A "continue" within a "for" loop causes control flow to jump to the execution of the expression statement (see for statement):

	e3;

which occurs implicitly after the loop body. After execution of this statement control is as normal, jumping backwards to the test of 'e2' at the top of the loop.


© 1991-2008 Prem Sobel. All Rights Reserved.