You should use the break statement judiciously, and with thought to the clarity of the resulting code.
Arbitrarily jumping out of loops or statements can obscure the flow of control in the code, making it harder to
understand and maintain. Some programming styles ban the use of break altogether, but that is an extreme
position. Contorting the logic in a loop to avoid the need for a break can result in code that is less clear than
it would have been with the break.
10.8. continue
A continue statement can be used only within a loop (for, while, or do) and transfers control to the end
of the loop's body to continue on with the loop. In the case of while and do loops, this causes the loop
expression to be the next thing evaluated. In a basic for loop continue causes the update-expression to be
evaluated next and then the loop expression. In the enhanced for loop execution goes to the next element in
the set of values if there is one.
Like the break statement, the continue statement has an unlabeled form:
continue;
and a labeled form:
continue label;
In the unlabeled form, continue transfers control to the end of the innermost loop's body. The labeled form
transfers control to the end of the loop with that label. The label must belong to a loop statement.
A continue is often used to skip over an element of a loop range that can be ignored or treated with trivial
code. For example, a token stream that included a simple "skip" token might be handled this way:
while (!stream.eof()) {
token = stream.next();
if (token.equals("skip"))
continue;
// ... process token ...
}
A labeled continue will break out of any inner loops on its way to the next iteration of the named loop. No
label is required on the continue in the preceding example since there is only one enclosing loop. Consider,
however, nested loops that iterate over the values of a two-dimensional matrix. Suppose that the matrix is
symmetric (matrix[i][j]==matrix[j][i]). In that case, you need only iterate through half of the
matrix. For example, here is a method that doubles each value in a symmetric matrix:
static void doubleUp(int[][] matrix) {
int order = matrix.length;
column:
for (int i = 0; i < order; i++) {
for (int j = 0; j < order; j++) {
matrix[i][j] = matrix[j][i] = matrix[i][j]*2;
if (i == j)
continue column;