Java break & continue
In this page:
for (initialization; condition; update) {
if (condition1) break; // exit loop
if (condition2) continue; // next iteration
}
The break Statement
break exits its enclosing loop immediately, skipping any remaining iterations entirely and jumping straight to the first statement after the loop's closing brace.
Example: The break Statement
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Loop: repeat while the condition holds
for (int i = 0; i < 5; i++) {
// Check whether i == 3
if (i == 3) {
// Leave the switch/loop
break;
}
// Print a line to the console
System.out.println(i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
The continue Statement
continue skips only the rest of the current iteration's code, then jumps straight to the loop's next condition check or update step -- unlike break, the loop itself keeps running afterward.
Example: The continue Statement
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Loop: repeat while the condition holds
for (int i = 0; i < 5; i++) {
// Check whether i == 2
if (i == 2) {
// Skip the rest of this iteration
continue;
}
// Print a line to the console
System.out.println(i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Labeled break
A labeled break (like outer: for (...) { for (...) { break outer; } }) lets code inside a nested inner loop terminate a specific outer loop directly, which a plain break -- which only affects its immediate loop -- can't do.
Example: Labeled break
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
outer:
for (int i = 0; i < 3; i++) {
// Loop: repeat while the condition holds
for (int j = 0; j < 3; j++) {
// Check whether j == 1
if (j == 1) {
break outer;
}
// Print a line to the console
System.out.println(i + "," + j);
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
Labeled continue
A labeled continue works the same way but skips to the next iteration of the named outer loop rather than the inner one, letting you abandon the current inner-loop pass and resume the outer loop's cycle.
Example: Labeled continue
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
outer:
for (int i = 0; i < 3; i++) {
// Loop: repeat while the condition holds
for (int j = 0; j < 3; j++) {
// Check whether j == 1
if (j == 1) {
continue outer;
}
// Print a line to the console
System.out.println(i + "," + j);
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
break in switch-case
Inside a switch-case, break stops execution from falling through into the next case block -- functionally similar to how it exits a loop, but here it's exiting the switch statement instead.
Example: break in switch-case
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare day and set it to 2
int day = 2;
// Choose a branch based on the value of day
switch (day) {
case 1:
System.out.println("Monday");
// Leave the switch/loop
break;
case 2:
System.out.println("Tuesday");
// Leave the switch/loop
break;
// default runs when no case matches
default:
System.out.println("Other");
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Using
breakinside aswitchinside a loop and expecting it to stop the loop, when it only exits the switch. - Putting code after
breakorcontinuein the same block, which is unreachable and a compile error. - Using
continuein awhileloop without updating the counter first, which creates an infinite loop.
if,if-else, else-if ladders, andswitchchoose which code runs based on conditions.while,do-while,for, and for-each loops repeat code, and for-each is convenient for collections and arrays.breakandcontinuecontrol how a loop exits or moves on.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: