- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
Java break Keyword
Usage and Examples
break
Keyword in Java
In Java, the break
keyword plays a powerful role in controlling the flow of a program. It's commonly used to exit a loop or a switch statement prematurely — meaning, before it has completed all its iterations or cases.
Sometimes in programming, you don't want to go all the way through a loop. Perhaps you found what you were looking for — no need to continue. That’s where break
steps in and says, “We’re done here.”
Where Can break
Be Used?
- Inside loops like
for
,while
, anddo-while
- Inside switch statements to exit after a matching case
- With labels to break out of nested loops (advanced use)
Using break
in a for
Loop
Let’s say you're looking for the number 5 in a list. Once found, you don't want to keep looping.
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
System.out.println("Found 5, breaking the loop!");
break;
}
System.out.println("i = " + i);
}
System.out.println("Outside the loop");
}
}
i = 1
i = 2
i = 3
i = 4
Found 5, breaking the loop!
Outside the loop
Explanation
The loop starts at 1 and runs up to 10, but the moment it hits i == 5
, the break
keyword causes it to stop entirely. Execution then continues after the loop.
Using break
in a while
Loop
public class BreakWhile {
public static void main(String[] args) {
int i = 1;
while (true) {
System.out.println("i = " + i);
if (i == 3) {
System.out.println("Reached 3, exiting the loop.");
break;
}
i++;
}
}
}
i = 1
i = 2
i = 3
Reached 3, exiting the loop.
Explanation
Even though this loop is infinite (while(true)
), the break
gives us an emergency exit. It’s the “manual override” for situations where you know you need to leave early.
Using break
in a switch
Statement
public class BreakSwitch {
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("Unknown day");
}
}
}
Wednesday
Explanation
Without break
, all the cases after a match would execute — a behavior called "fall-through." By using break
, we prevent that and exit the switch after the first match.
Using break
with Labels (Advanced)
In Java, you can label loops and use break
to exit from outer loops, not just the inner one.
public class BreakWithLabel {
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; // breaks the outer loop
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
Explanation
Here, the labeled break skips not just the inner loop, but jumps completely out of the outer loop too when i == 2
and j == 2
. This is useful in nested logic when you want to escape multiple levels at once.
Summary
- The
break
keyword is used to exit loops or switch cases early. - It's extremely useful when you meet a condition and don’t need to continue.
- When used inside a nested structure,
break
can target outer loops using labels.
When Should You Use break
?
Use break
when continuing a loop no longer makes sense. Whether you're done searching, responding to a user action, or simplifying complex logic — break
can be your escape hatch.