⬅ Previous Topic
Java Do-While LoopNext Topic ⮕
Java Continue Statement⬅ Previous Topic
Java Do-While LoopNext Topic ⮕
Java Continue StatementThe break
statement in Java is used to prematurely exit loops or switch-case blocks. It's a flow control mechanism used when you want to stop the current loop execution and jump out of it completely, regardless of the loop’s condition.
break
?Imagine you're searching for a specific value in a list. Once it's found, there’s no reason to continue looping through the rest of the items. This is where break
shines — it enhances performance and avoids unnecessary operations.
break
break;
The break
statement is written on its own and ends with a semicolon. It can be placed inside for, while, or do-while loops, as well as switch blocks.
break
in a for
LoopLet’s look at a simple example where we loop through numbers but stop as soon as we hit number 5.
public class BreakExample1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println("i = " + i);
}
System.out.println("Loop exited using break.");
}
}
i = 1
i = 2
i = 3
i = 4
Loop exited using break.
The loop prints numbers starting from 1. When i
reaches 5, the condition i == 5
becomes true. The break
statement executes, exiting the loop immediately. Any statements after the loop continue to execute as normal.
break
in a while
LoopThis time, we use a while
loop and break out of it when a condition is met.
public class BreakExample2 {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
if (i == 6) {
break;
}
System.out.println("i = " + i);
i++;
}
}
}
i = 1
i = 2
i = 3
i = 4
i = 5
break
in a switch
StatementIn switch
statements, break
is used to terminate a case block and prevent fall-through.
public class BreakInSwitch {
public static void main(String[] args) {
int day = 3;
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("Another day");
}
}
}
Wednesday
When break
is used inside nested loops, it only exits the innermost loop by default.
public class BreakNested {
public static void main(String[] args) {
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);
}
}
}
}
i = 1, j = 1
i = 2, j = 1
i = 3, j = 1
To break out of multiple nested loops at once, you can use a labeled break
. Here's how:
public class LabeledBreak {
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);
}
}
}
}
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
break
?switch
statementsbreak
outside a loop or switch — this will result in a compile-time errorbreak
— it can make code harder to follow if used excessivelyThe break
statement in Java is your exit gate from loops and switch blocks. It gives you the flexibility to stop a loop when it’s no longer needed, saving processing time and simplifying logic. However, use it wisely to keep your code clean and maintainable.
break;
statement is encountered in a loop?break
statement can be used in both loops and switch-case constructs in Java.for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.print(i + " ");
}
What is the output?break
in Java are correct?break
inside a switch
statement prevents execution from falling through to the next case.outer:
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
break outer;
}
System.out.println("i = " + i + ", j = " + j);
}
}
How many lines are printed?⬅ Previous Topic
Java Do-While LoopNext Topic ⮕
Java Continue StatementYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.