Control Statements
Break and continue
continue
The syntax of the continue statement
continue;
How to code continue statements
- To skip the rest of the statements in the current loop and jump to the top of the current loop, you can use the continue statement.
A continue statement that jumps to the beginning of a loop
for (int j = 1; j < 10; j++) { int number = (int) (Math.random() * 10); System.out.println(number); if (number <= 7) continue; System.out.println("This number is greater than 7"); }
The syntax of the labeled continue statement
continue labelName;
The structure of the labeled continue statement
labelName: loop declaration { statements another loop declaration { statements if (conditionalExpression) { statements continue labelName; } } }
A labeled continue statement that jumps to the beginning of the outer loop
outerLoop: for(int i = 1; i < 20; i++) { for(int j = 2; j < i-1; j++) { int remainder = i%j; if (remainder == 0) continue outerLoop; } System.out.println(i); }