The continue statement is used to transfer control from inside a loop to the end of the loop but does not exit the loop like the break statement does. The continue statement consists of the keyword, continue
.
When executed, it forces the evaluation of the loop's logical expression. In the following statement sequence:
while (i < j) { … if (i < 0) { continue; } … }
if i
is less than 0
, it will bypass the rest of the body of the loop. If the loop condition i<j
does not evaluate to false, the next iteration of the loop will be performed.
The continue statement is often used to eliminate a level of nesting which is often necessary. The preceding example would appear as follows, if the continue statement was not used:
while (i < j) { … if (i < 0) { // Do nothing } else { … } }