Break statement java – Java Break Statement with Example | How do you Insert a Break in a Statement in Java?

Break statement java: A programming language uses control statements to control the flow of execution of a program. In Java programming, we can control the flow of execution of a program based on some conditions. Java control statements can be put into the following three categories: selection, iteration, and jump. In this tutorial, we will learn about the Java jump statement. The jump statement can be used to transfer the control to other parts of the program. Java break statement is one of the jump statements.

Java Break Statement

Java break statement: Java break statements can be used to terminate the loop. It can be used inside a loop. We can use a break statement in all types of loops such as: while loop, do-while loop, and for a loop. When a break statement is encountered inside a loop, the loop is terminated and the program control goes to the next statement following the loop. In java, the break statement has three uses.

  1. It terminates a statement sequence in a switch statement.
  2. It can be used to exit a loop.
  3. It can be used as a “civilized” form of goto (labeled break).

Syntax:

break;

Flowchart of Break Statement

Java break statement with Example 1.

Also, Check:

How does Break Statement Work?

Working of Java Break Statement

Use of Break in a While Loop

Java label break: In the below example we are having a while loop that runs from 0 to 100 and as we have a break statement that only reaches when the loop reaches 2, the loop gets terminated and control passes to the next statement after the loop body.

public class BreakExample1 {
public static void main(String args[]){
int num =0;
while(num<=100)
{
System.out.println("Value of variable is: "+num);
if (num==2)
{
break;
}
num++;
}
System.out.println("Out of while-loop");
}
}

Output:

Value of variable is: 0
Value of variable is: 1
Value of variable is: 2
Out of while-loop

Use of Break in a For Loop

What does a break do in java: Soon after the var hits the var value 99 loop gets terminated.

public class BreakExample2 {

public static void main(String args[]){
int var;
for (var =100; var>=10; var --)
{
System.out.println("var: "+var);
if (var==99)
{
break;
}
}
System.out.println("Out of for-loop");
}
}

Output:

var: 100
var: 99
Out of for-loop

Use of Break Statement in Switch Case

Java break label: In the below program we are having a break statement after every case this is because if we don’t have it the subsequent case blocks would also execute. The same Program without break statement after each case would be Case 2 Case 3 Default.

public class BreakExample3 {

public static void main(String args[]){
int num=2;

switch (num)
{
case 1:
System.out.println("Case 1 ");
break;
case 2:
System.out.println("Case 2 ");
break;
case 3:
System.out.println("Case 3 ");
break;
default:
System.out.println("Default ");
}
}
}

Output:

Case 2

Java Break Nested Loop

Break Statement with Nested Loops

Using the Break to Exit a loop

When a break statement is encountered inside a loop, the loop is terminated and the program control goes to the next statement following the loop. See the example below:

Java Break Statement to Exit a Loop with Example

class BreakLoop{
public static void main(String args[]){
for(int i = 1; i<=10; i++){
if(i==6)
break;
System.out.println("i is: " +i);
}
System.out.println("Loop Complete !");
}
}

Output:

Java break statement with Example 2

Java Labeled Break Statement

The break statement can also be used by itself to provide a “civilized” form of the goto statement. Java doesn’t provide a goto statement, because it provides a way to branch in any arbitrary and unstructured manner. Java uses the label. A label is a block of code that must enclose the break statement, but it doesn’t need to be immediately enclosing block. This means that you can use a labeled break statement to exit from a set of nested blocks.

Labeled Break Statements

Java Labeled Break Statement with Example

class LabeledBreakLoop{
public static void main(String args[]){
boolean b = true;

// first lable.
first:{
//second lable.
second:{
//third label.
third:{
System.out.println("Before the break statement.");
if(b)
break second;
System.out.println("This would not execute! ");
}
System.out.println("This would not execute! ");
}
System.out.println("This is after the second block.");
}
}
}

Output:

Before the break statement.
This is after the second block.