Você está na página 1de 4

It's possible to force an early iteration of a loop, bypassing the loop's normal control

structure. This is accomplished using continue. The continue statement forces the next
iteration of the loop to take place, skipping any code between itself and the conditional
expression that controls the loop. Thus, continue is essentially the complement of break.
For example, the following program uses continue to help print the even numbers
between 0 and 100.

Only even numbers are printed, because an odd one will cause the loop to iterate early,
bypassing the call to println( ). In while and do-while loops, a continue statement will
cause control to go directly to the conditional expression and then continue the looping
process. In the case of the for, the iteration expression of the loop is evaluated, then the
conditional expression is executed, and then the loop continues.
As with the break statement, continue may specify a label to describe which enclosing
loop to continue. Here's an example program that uses continue with a label:
This is the output from the ContToLabel program:

As the output shows, when the continue executes, control passes to the outer loop,
skipping the remainder of the inner loop. Good uses of continue are rare. One reason is
that Java provides a rich set of loop statements that fit most applications. However, for
those special circumstances in which early iteration is needed, the continue statement
provides a structured way to accomplish it.
Summary

In this lesson, you learned that it's possible to bypass a loop's normal control structure
and force an early iteration of the loop by using the continue statement.

You also learned that the continue statement can be used to skip any code between itself
and the conditional expression that controls the loop thus forcing the next iteration of the
loop to take place.