The break statement in Java is used to stop a loop
immediately. When Java executes break, it exits the
nearest enclosing loop and continues with the statement after that loop.
Suppose you want to print numbers from 1 to 10, but you want the loop to stop when the number reaches 5.
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
Output:
1
2
3
4
i == 5 becomes true.
break;
Example:
int i = 1;
while (i <= 10) {
if (i == 4) {
break;
}
System.out.println(i);
i++;
}
Output:
1
2
3
The break statement exits the loop immediately when
i becomes 4.
Print numbers from 1 to 10, but stop at 6.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 6) {
break;
}
System.out.println(i);
}
}
}
Output:
1
2
3
4
5
When i becomes 6, the loop terminates.
The remaining iterations are skipped.
int i = 1;
while (i <= 10) {
if (i == 5) {
break;
}
System.out.println(i);
i++;
}
Output:
1
2
3
4
The while loop stops when the value of i
becomes 5.
int i = 1;
do {
if (i == 4) {
break;
}
System.out.println(i);
i++;
} while (i <= 10);
Output:
1
2
3
The do-while loop stops when i == 4.
The condition at the end is not checked after the break exits
the loop.
The break statement is useful when searching for an item. Once the required number is found, the loop can stop.
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int target = 30;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == target) {
System.out.println("Number found!");
break;
}
}
}
}
Output:
Number found!
The loop stops searching after finding the first matching number.
Search for the letter 'a' in a word.
public class Main {
public static void main(String[] args) {
String word = "computer";
boolean found = false;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 't') {
found = true;
break;
}
}
if (found) {
System.out.println("Character found!");
} else {
System.out.println("Character not found!");
}
}
}
Output:
Character found!
The loop stops when the character 't' is found. The boolean variable records the search result.
A nested loop is a loop inside another loop. An ordinary break statement exits only the nearest enclosing loop.
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);
}
}
Output:
i = 1, j = 1
i = 2, j = 1
i = 3, j = 1
Each inner loop stops when j becomes 2,
but the outer loop continues.
In a traditional Java switch statement, break is commonly used to stop execution from falling through into the next case.
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
Output:
Tuesday
Here, break exits the switch statement rather than a loop.
This program keeps asking the user to enter a number until the user enters zero.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Enter a number (0 to stop): ");
int number = sc.nextInt();
if (number == 0) {
break;
}
System.out.println("You entered: " + number);
}
System.out.println("Program stopped.");
sc.close();
}
}
Example interaction:
Enter a number (0 to stop): 5
You entered: 5
Enter a number (0 to stop): 8
You entered: 8
Enter a number (0 to stop): 0
Program stopped.
The loop continues until the user enters zero.
Then break terminates the loop.
Java supports a labeled break that can exit an enclosing labeled loop, including from inside a nested loop.
public class Main {
public static void main(String[] args) {
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
break outer;
}
System.out.println("i = " + i + ", j = " + j);
}
}
System.out.println("Loops stopped.");
}
}
Output:
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
Loops stopped.
The statement break outer; exits both loops
by transferring control outside the labeled outer loop.
| Break | Continue |
|---|---|
| Terminates the nearest enclosing loop. | Skips the current iteration and proceeds to the next one. |
| Execution continues after the loop. | The loop continues if its condition allows. |
| Example: stop printing at 5. | Example: skip printing 5 but continue with later numbers. |
Break example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
Output:
1
2
Continue example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
Write Java programs to complete these assignments.
1. What does the break statement do in a loop?
2. What is the output of this code?
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
3. What does a labeled break allow you to do?