Break Statement in Java | Example Program

Jump Statements

Java supports three types of jump statements: break, continue, and return. These statements transfer control of execution to another part of the program.

For example, suppose there is a list of 100 names and we need to search a particular name from the list.

When the search is completed and the desired name is found, the program loop written for searching must be terminated.

For this purpose, Java allows a jump from one statement to the end or beginning of a loop as well as jump out of a loop.

So, let’s understand all three types of jump statements one by one.

Break Statement in Java


A break statement in Java is used to break a loop or switch statement. When a break statement is executed inside a loop or switch statement, the loop is immediately terminated at a specified condition and the program control continues the next statement immediately following the loop.

When the loops are nested, break statement breaks only the inner loop. Java break statement can be used in all types of loops such as for loop, while loop, and do-while loop.

The general syntax for break statement is as follows:

// Jump statement
   break;

The flowchart of break statement in java is shown in the below figure.

Flowchart of break statement in Java

A break statement in Java can be used in three ways that are as follows:


1. A break can be used inside the loop to come out of it.

Existing a loop with break statement in Java

2. It can be used inside the switch block to come out of switch block.

3. It is also used inside the nested blocks to go to the end of block.

Example Program based on Break Statement


1. Let’s take a simple example program where we will use a break statement inside the for loop for breaking the loop. Look at the program source code.

Program source code 1:

package javaProgram;
public class BreakExample1 {
public static void main(String[] args) 
{
// Using for loop.
  for(int i = 1; i <= 10; i++)
  {
   if(i == 5) break; //  Breaking a loop.
    System.out.println("I: " +i);
  }
 }}
Output:
            I: 1
            I: 2
            I: 3
            I: 4

2. Let’s take another example program where we will use break statement inside the inner for loop.

Program source code 2:

package javaProgram;
public class BreakExample2 {
public static void main(String[] args)
{
// Outer for loop.
  for(int i = 1; i <= 3; i++)
  {
 // Inner for loop.
   for(int j = 0; j <= 3; j++){
   if(i == 2 && j == 2)
     break; // Using break statement inside for loop.
   System.out.println(i+" "+j);
   }   
  }
 }}
Output:
             1 0
             1 1
             1 2
             1 3
             2 0
             2 1
             3 0
             3 1
             3 2
             3 3

3. Let’s take an example program where we will use break statement with labeled for loop. We can use break with the label. This feature is added since JDK 1.5 version.

Program source code 3:

package javaProgram;
public class BreakExample3 {
public static void main(String[] args)
{
// Outer for loop.
  for(int i = 1; i <= 3; i++)
  {
   bb:  
 // Inner for loop.
   for(int j = 1; j <= 3; j++){
    if(i == 2 && j == 2)
      break bb; // Using break statement with label.
  System.out.println(i+" "+j);
   }   
  }
 }}
Output:
             1 2
             1 3
             2 1
             3 1
             3 2
             3 3

4. Let’s create a program where we will use break statement inside while loop.

Program source code 4:

package javaProgram;
public class BreakWhileEx {
public static void main(String[] args)
{
// while loop.
  int i = 1;
  while(i <= 10){
   if(i == 5){
    i++;
    break; // Breaking the loop.
   }
 System.out.println(i);	
 i++;
  }
 }
}
Output:
             1
             2
             3
             4

5. Let’s create a program where we will use break statement inside the do-while loop.

Program source code 5:

package javaProgram;
public class BreakDoWhileEx {
public static void main(String[] args)
{
// Initialization.
int i = 1;
// do-while loop.
 do { 
 if(i == 5)	{
  i++;
  break;
 }
System.out.println(i);
 i++; 
 } while(i <= 10);
 }
}
Output:
             1
             2
             3
             4

Hope that this tutorial has covered almost all the important points related to break statement in Java with example programs. I hope that you will have understood the basics of break statement and practiced programming.
Thanks for reading!!!
Next ⇒ Continue statement in Java

⇐ Prev Next ⇒