
In Java, the break statement is a powerful tool for controlling program flow and terminating loops. It allows you to exit out of loops prematurely or skip the remaining code within a loop or switch statement.
There are two types of break statements in Java:
- Unlabeled
breakstatement - Labeled
breakstatement
The unlabeled break statement is used to terminate and exit the loop constructs. It allows you to skip the remaining statements within the loop and continue execution after the enclosing statement.
The labeled break statement allows you to terminate and exit an outer loop or a labeled statement. It provides more control over which statement to break out of when there are multiple nested loop constructs.
Here are examples of using the unlabeled break statement in different loop types:
break in for Loop
This is the code snippet for using unlabeled break statement in for loop.
for (int count = 1; count <= 10; count++) {
if (count == 6) {
break;
}
System.out.println(count);
}
Output:
1
2
3
4
5
break in Enhanced for Loop
This is the code snippet for using unlabeled break statement in enhanced for loop.
int[] counts = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int count: counts) {
if (count == 6) {
break;
}
System.out.println(count);
}
Output:
1
2
3
4
5
break in while Loop
This is the code snippet for using unlabeled break statement in while loop.
int count1 = 1;
while (count1 <= 10) {
if (count1 == 6) {
break;
}
System.out.println(count1);
count1++;
}
Output:
1
2
3
4
5
break in do-while Loop
This is the code snippet for using unlabeled break statement in do-while loop.
int count2 = 1;
do {
if (count2 == 6) {
break;
}
System.out.println(count2);
count2++;
} while (count2 <= 10);
Output:
1
2
3
4
5
break in switch Statement
This is the code snippet for using unlabeled break statement in switch statement.
String day = "Thursday";
switch (day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
System.out.printf("%s is a weekday%n", day);
break;
case "Saturday":
case "Sunday":
System.out.printf("%s is a weekend%n", day);
break;
default:
System.out.printf("%s is an invalid day%n", day);
}
Output:
Thursday is a weekday
String type argument to switch statement to compare the value.Labeled break Statement
Here’s an example using a labeled break statement to terminate and exit the outer for loop.
LabeledBreakStatementExample.java
public class LabeledBreakStatementExample {
public static void main(String[] args) {
outerLoop:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (i == 3 && j == 3) {
break outerLoop;
}
System.out.printf("i = %d, j = %d%n", i, j);
}
}
}
}
Output:
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 1, j = 4
i = 1, j = 5
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 2, j = 4
i = 2, j = 5
i = 3, j = 1
i = 3, j = 2
Summary
In this tutorial, we’ve covered the usage of both unlabeled and labeled break statements in Java. You’ve learned how to terminate loops prematurely, skip iterations, and gain more control over program flow.
Mastering the break statement is essential for writing efficient and concise code in Java. It allows you to design loops and switch statements that break out of specific conditions or terminate when certain criteria are met.
You can find the example codes for this tutorial on GitHub.