Home  >  Article  >  Java  >  Break Statement in Java

Break Statement in Java

PHPz
PHPzOriginal
2024-08-30 15:24:02427browse

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.

ADVERTISEMENT Popular Course in this category FINANCIAL MODELING & VALUATION - Specialization | 51 Course Series | 30 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

Break Statement in Java

 

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:

Break Statement in Java

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:

Break Statement in Java

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:

Break Statement in Java

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:

Break Statement in Java

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:

Break Statement in Java

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.

The above is the detailed content of Break Statement in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn