← Back to Core Java Course | Chapter 4: Control Flow | Lesson 9 of 9

Java break & continue

break leaves a loop right away, like walking out of a game once you win, and continue skips only the current round. Labels let you choose which loop to leave.
Syntax
java
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

java
// 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);
		}
	}
}

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

java
// 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);
		}
	}
}

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

java
// 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);
			}
		}
	}
}

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

java
// 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);
			}
		}
	}
}

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

java
// 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");
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using break inside a switch inside a loop and expecting it to stop the loop, when it only exits the switch.
  2. Putting code after break or continue in the same block, which is unreachable and a compile error.
  3. Using continue in a while loop without updating the counter first, which creates an infinite loop.
Chapter Summary
  • if, if-else, else-if ladders, and switch choose 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.
  • break and continue control how a loop exits or moves on.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.