Updated March 17, 2023
Introduction to Break Statement in Java
The break statement is a keyword to terminate the execution of the instructions in the loop; code execution remains to continue after the loop. The break statement is one of the best ways to terminate the execution in the loop. Break statement in java can be used in multiple use cases; the best use case of the break statement comes in the loop where it terminates the execution of code inside the loop & code execution resumes after the loop. Another example of a break statement is in the switch case.
Example:
switch(value) {
case 1:
// Statements
break;
case 2:
// Statements
break;
case 3:
// Statements
break;
default :
//Statements
}
Syntax:
The break statement is a simple one-line statement as given below. In the above-given switch case syntax, we can see how the break is placed at the end of each case to terminate execution inside of the loop.
break;
Flow Diagram
In the below-given diagram, we can see how to break the statement terminates the execution inside the loop/switch statement & execution jumps to the next step after the loop.
How does Break Statement work in Java?
- Break statement jumps the code compilation to the outside of the loop, while loop, do-while, for-each, switch, & other conditional statements.
- The break statement closes the loop where it is placed. If a break statement is not labeled and placed inside the loop, it will jump code execution outside of that loop.
- In the labeled loop, the break statement is placed with the label. Break statement with label jumps program execution control to outside of that labeled code block.
- In Java, the break statement also works the same as in other programming languages.
Example #1
See the example of how execution is being terminated after successful execution of if statement & program execution control moves to the next step.
public class BreakStatementExample{
public static void main(String[] args){
// Create frame with title Registration Demo
int i=1;
while(i < 10){
if(i == 5){
break;
}
// Printing the counter
System.out.print(i);
System.out.print("\n");
i++;
}
}
}
In the above-given example, In the loop, once the counter reaches up to 5, then the break statement executes. Once the break statement executes, the further counter will not display as the break statement made execution to out of the loop. We can see the expected result in the attached screenshot below.
Output:
Example #2
In this example, we can see how execution jumps to the outside of the switch case statement once any case gets true.
public class BreakStatementSwitchCaseExample{
public static void main(String[] args){
int y=2;
showExecution(y);
}
public static void showExecution(int i){
switch(i){
case 1:
System.out.print("Case 1 Executed");
break;
case 2:
System.out.print("Case 2 Executed");
break;
case 3:
System.out.print("Case 1 Executed");
break;
default:
System.out.print("Default Case Executed");
}
}
}
In the above-given example break statement is placed in each case of the switch statement. Once any case code block executed, break statement stop execution at that point & resume the code execution outside of the switch statement.
Output:
Example #3
In this example, the break statement is placed inside the inner loop. Whenever condition met & break statement executes, code execution jumps program execution control to outside of the inner loop.
public class LabeledBreakStatement{
public static void main(String[] args){
for(int i=1; i < 3; i++){
for(int j=1; j < 4; j++){
System.out.print(i +" "+ j);
System.out.print("\n");
if(j == 2){
break;
}
}
}
}
}
In the below-given screenshot, how the inner loop is terminated after printing 2.
Output:
Example #4
In the below-given example, we can see a labeled break statement example. Labeled break refers to a particular code block. When the labeled break statement executes execution, controls move out of the labeled code block.
public class LabeledBreakStatement{
public static void main(String[] args){
primary:
for(int i=1; i < 3; i++){
secondary:
for(int j=1; j < 4; j++){
System.out.print(i +" "+ j);
System.out.print("\n");
if(j == 2){
break primary;
}
}
}
}
}
In the above-given example, each loop starts after a label. In certain conditions, the break statement is terminating that labeled loop. & execution control moves to the outside of the labeled. We can see the output of the program in the below-given screenshot.
Output:
Example #5
Moving to the next example, This example has the example of a do-while statement & also labeled break statement in it.
public class DoWhileBreakStatement{
public static void main(String[] args){
int j = 1;
do{
if(j == 6){
break;
}
System.out.println(j);
j++;
}
while(j < 9);
System.out.println("\n\n");
labeledBreakLoopExample();
//Further Checking execution moves to this step or not
}
//Labeled statement with nested loop
public static void labeledBreakLoopExample(){
primary:
for(int i=1; i < 3; i++){
secondary:
for(int j=1; j < 4; j++){
System.out.print(i +" "+ j);
System.out.print("\n");
if(j == 2){
break secondary;
}
}
}
}
}
In the above-given example, we can see that the method will print the first counter up to 5. After execution of break statement when the counter reaches 6, program execution control executes the if case & entered into the if case inside code. Break statement execution inside of the if condition terminates the execution of loop & moves execution control to the next step.
Further next method executes. This method has labeled statements. This method executes the break statement with a secondary label on certain true conditions, which closes the execution of a secondary labeled loop & jumps program execution outside of the internal labeled loop.
Output:
Advantages
- A loop can be easily closed by using a break statement.
- It can be utilized in any type of loop-like for, foreach, while, do-while, etc.
- The break statement can be used with a labeled loop. The labeled break statement can be used to terminate that labeled loop execution further. That labeled loop may be inner or at the top label in the statements.
- Break statement makes the loop more flexible & provides more power to it.
Conclusion – Break Statement in Java
The break is the reserved java keyword. It is one of the keywords which is used by programmers for termination of execution instantly in the loop or in conditional statements & program execution control moves to the next step. Its better use case scenario comes under the decision making statements.
Recommended Articles
This is a guide to Break Statement in Java. Here we discuss the introduction to Break Statement in Java and how break statements work in java, along with its example. You may also look at the following articles to learn more-