⏱️ 8 min read • Beginner Level • Lesson 30
Completed on . You can revise this lesson or continue to the next topic.
The break statement in Java is used to stop the current loop or switch statement immediately. It is like pressing an emergency stop button when your program has already found what it needs.
Before learning the break statement, you should understand Java for loop, Java while loop, Java do-while loop, and Java switch case.
In Java, the break statement is used to terminate the nearest
loop or switch statement. Once Java executes break, control moves
to the statement immediately after that loop or switch block.
break stops a loop before its normal ending condition.
break prevents fall-through in switch statements.
break is useful when searching, validating input, stopping menus, or exiting nested loops.
The syntax of break is very simple:
break;
break statement can be used inside loops and switch statements.
It cannot be used directly outside a loop, switch, or labeled block.
The flowchart of break statement is shown below:
break statement, it immediately stops the nearest loop or switch.
In this example, the loop should normally print numbers from 1 to 10,
but we stop it when the value becomes 5.
public class BreakInForLoop {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
System.out.println("Loop stopped.");
}
}
Output:
1 2 3 4 Loop stopped.
i = 1.1, 2, 3, and 4.i becomes 5, the condition i == 5 becomes true.break statement executes and stops the loop immediately.Loop stopped.
This example stops a while loop when the counter reaches 4.
public class BreakInWhileLoop {
public static void main(String[] args) {
int count = 1;
while (count <= 10) {
if (count == 4) {
break;
}
System.out.println("Count: " + count);
count++;
}
System.out.println("Stopped at count 4.");
}
}
Output:
Count: 1 Count: 2 Count: 3 Stopped at count 4.
The break statement is commonly used in menu-driven programs.
In this example, the menu keeps running inside an infinite while(true) loop.
When the user enters 2, the break statement stops the loop and terminates the program.
import java.util.Scanner;
public class ExitMenu {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1. Continue");
System.out.println("2. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
if (choice == 2) {
break;
}
System.out.println("Menu continues...");
}
System.out.println("Program terminated.");
sc.close();
}
}
Sample Input:
1 2
Output:
1. Continue 2. Exit Enter your choice: 1 Menu continues... 1. Continue 2. Exit Enter your choice: 2 Program terminated.
Scanner class from the java.util package.
Scanner object named sc is created to read input from the keyboard.
while(true), which creates an infinite loop.
1. Continue and 2. Exit.
sc.nextInt().
1, the condition choice == 2 is false,
so the program prints Menu continues... and shows the menu again.
2, the condition choice == 2 becomes true.
break statement executes and immediately stops the while loop.
Program terminated..
sc.close() closes the Scanner object.
while(true) creates an infinite loop. The break statement is used to safely exit the loop when a specific condition is met.
In a switch statement, break is used to stop fall-through.
Without break, Java may continue executing the next cases.
public class BreakInSwitch {
public static void main(String[] args) {
int choice = 2;
switch (choice) {
case 1:
System.out.println("Start Game");
break;
case 2:
System.out.println("Open Settings");
break;
case 3:
System.out.println("Exit Game");
break;
default:
System.out.println("Invalid choice");
}
}
}
Output:
Open Settings
switch statement, break prevents Java from executing
the next case accidentally.
Imagine you are searching for a roll number in a list. Once the roll number is found,
there is no need to continue checking the remaining elements. This is a perfect use case
for break.
public class SearchRollNumber {
public static void main(String[] args) {
int[] rollNumbers = {101, 102, 103, 104, 105};
int search = 103;
boolean found = false;
for (int roll : rollNumbers) {
System.out.println("Checking roll number: " + roll);
if (roll == search) {
found = true;
System.out.println("Roll number found: " + roll);
break;
}
}
if (!found) {
System.out.println("Roll number not found.");
}
}
}
Output:
Checking roll number: 101 Checking roll number: 102 Checking roll number: 103 Roll number found: 103
After finding roll number 103, the program stops the loop.
It does not waste time checking 104 and 105.
This makes the program more efficient.
In this example, the user gets limited password attempts. If the correct password is entered,
the loop stops immediately using break.
public class PasswordAttemptExample {
public static void main(String[] args) {
String[] attempts = {"java123", "hello", "admin@123"};
String correctPassword = "admin@123";
for (String password : attempts) {
System.out.println("Trying password: " + password);
if (password.equals(correctPassword)) {
System.out.println("Access granted!");
break;
}
}
}
}
Output:
Trying password: java123 Trying password: hello Trying password: admin@123 Access granted!
In nested loops, a normal break stops only the nearest inner loop.
The outer loop continues running.
public class BreakNestedLoop {
public static void main(String[] args) {
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
if (col == 2) {
break;
}
System.out.println("row = " + row + ", col = " + col);
}
}
}
}
Output:
row = 1, col = 1 row = 2, col = 1 row = 3, col = 1
col becomes 2, the inner loop stops.
Then the outer loop moves to the next row.
A labeled break can stop an outer loop directly. It is useful when you want to exit multiple nested loops at once.
public class LabeledBreakExample {
public static void main(String[] args) {
outerLoop:
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
if (row == 2 && col == 2) {
break outerLoop;
}
System.out.println("row = " + row + ", col = " + col);
}
}
System.out.println("Exited from outer loop.");
}
}
Output:
row = 1, col = 1 row = 1, col = 2 row = 1, col = 3 row = 2, col = 1 Exited from outer loop.
| break | continue |
|---|---|
| Stops the loop completely. | Skips only the current iteration. |
| Control moves outside the loop. | Control moves to the next iteration. |
| Used when no more loop execution is needed. | Used when only one iteration should be skipped. |
| Can be used in loops and switch. | Used only in loops. |
break outside a loop, switch, or labeled block.break inside a switch case and causing fall-through.break stops all nested loops automatically.break when continue is actually needed.What is the difference between break and continue in Java?
break terminates the loop completely, while continue skips only the current iteration and moves to the next iteration.
break statement stops the nearest loop or switch immediately.for, while, do-while, and switch.break exits only the inner loop.break can exit an outer loop directly.break when you no longer need to continue the loop.🧠 Test your understanding with a quick quiz
Topic: Break | Language: Java
int choice = 1;
switch (choice) {
case 1:
System.out.println("Case 1");
case 2:
System.out.println("Case 2");
default:
System.out.println("Default");
}int choice = 1;
switch (choice) {
case 1:
System.out.println("Case 1");
break;
case 2:
System.out.println("Case 2");
break;
default:
System.out.println("Default");
}for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
break;
}
System.out.println("i=" + i + ", j=" + j);
}
}for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.