Control Statements
Break and continue
break
The syntax of the break statement
break;
How to code labeled break statements
- To jump to the end of an outer loop from an inner loop, you can label the outer loop and use the labeled break statement.
A break statement that exits the inner loop
for (int i = 1; i < 4; i++) { System.out.println("Outer " + i); while (true) { int number = (int) (Math.random() * 10); System.out.println(" Inner " + number); if (number > 7) break; } }
How to code break statements
- To jump to the end of the current loop, you can use the break statement.
The syntax of the labeled break statement
break labelName;
The structure of the labeled break statement
labelName: loop declaration { statements another loop declaration { statements if (conditionalExpression) { statements break labelName; } } }
A labeled break statement that exits the outer loop
outerLoop: for (int i = 1; i < 4; i++) { System.out.println("Outer " + i); while (true) { int number = (int) (Math.random() * 10); System.out.println(" Inner " + number); if (number > 7) break outerLoop; } }