Introduction
In Java programming language, break and continue are called jump statements. The jumping statements are the control statements that transfer the program execution control to a specific statement. In Java, Jump statements are used to unconditionally transfer program control from one point to elsewhere. Jump statements are primarily used to interrupt loop or switch-case instantly.
The break Statement in Java
Sometimes, jumping out of a loop is necessary, irrespective of the conditional test value. The break statement is used inside any loop to allow the control to jump to the instant statement following the loop.
Syntax
if(condition)
{
break;
}
How can we use the break statement in Java?
When we use a nested loop, the break jumps the control from the loop where it has been used. The break statement can be used inside any loop, i.e., while, do-while, for, and switch statements. In Java, the use of break, the statement is to forcibly terminate the loop and resume at the next statement of the loop. As soon as the break statement is encountered inside the loop, that loop is immediately terminated without executing the remaining code in the body of that loop. The program control reaches the next statement written after that loop.
The break statement is different from the exit. Break jumps the control out of the loop, and exit stops the execution of the entire program. The complete program of the break statement is listed below.
public class BreakExample1 {
public static void main(String args[]) {
int[] test = {0, 1, 2, 3, 4};
for (int s : test) {
if (s == 3) {
break;
}
System.out.print(s);
System.out.print("\n");
}
System.out.println("break statement execution");
}
}
Output
Explanation
In this program, the for loop is executed starting from s = 0 to 4 in steps of 1. When the condition (i==3) in the loop's body is satisfied, the break statement causes the control to move out of the loop.
Here is one more example program for understanding the working of the break statement in Java. The complete program is listed below.
public class BreakExample3 {
//using the break to exit the loop
public static void main(String args[]) {
for (int i = 0; i < 100; i++) {
System.out.println(+i);
if (i == 10)
break; // as the condition inside 'if' satisfies control comes out of 'for' loop.
}
System.out.println(" Loop breaks here");
}
}
Output

Break statement with a nested loop
break only let the program exit the loop which encloses it. If the break is used with a nested loop, it breaks out only the innermost loop and does not affect the outer loop. It breaks the nested loop only if we use a break statement inside the nested loop. The complete program of break statements with a nested loop is listed below.
public class BreakExample2 {
public static void main(String[] args) {
for (int a = 1; a <= 3; a++) {
for (int b = 1; b <= 3; b++) {
if (a == 2 && b == 2) {
break;
}
System.out.println(a + " " + b);
}
}
}
}
Output
Explanation
In this program, the nested for loop execution started from a = 0 to 3 and b = 1 to 3 in steps of 1. When the condition (a == 2 && b ==2) in the loop's body is satisfied, the break statement causes the control to move out of the loop.
The labeled break statement
Java uses a break as a form of goto without its problem. As goto provides an unstructured branching which is hard to understand and also goto prohibits compiler optimization, Java uses an expanded form of break which is similar to goto and helps to exit more than one block at a time and resume the control of the program to the end of the labeled block, mentioned with the break statement.
Syntax
if(condition)
break label_name;
The complete program is listed below.
public class BreakExample4 {
//using break as a form of goto
public static void main(String args[]) {
boolean t = true;
first:{
second:{
third:{
System.out.println(" this third block");
if (t) break first;
}
System.out.println(" this second block");
}
System.out.println(" this first block");
}
System.out.println(" this main block");
}
}
Output




Mageshwaran RPosted Nov 16, 2019, 8:46 AM
Great,very useful to me.
Gaurav KumarPosted Sep 27, 2019, 12:13 AM
Very nice article, glad to know various use-cases of continue and break. Keep sharing ...