×

Java Break Keyword

Break:

  • The word Break is a keyword or a statement in a java programming language.
  • This Keyword break is used to stop the execution of the loop or switch statements etc.
  • The loop is stopped by the break statement, so it is mandatory to end the loop.
  • When a break statement is approaches a loop, the reputation of the loop terminates and starts backward from the loop to the statement of the starting loop.
  • When we are not sure about the reputations of the loop then basically we use the break statement.
  • Break keyword controls the Structures and switch statements in the loop.  loops and switch statements in java are terminated by using break keyword
  •  When the loop encounters the break word within a loop, the loop gets terminated and the controls of the program shift to the next statement it follows the loop
  •  The inner loop will stop execution or end the program or terminate the loop only. When a break keyword implemented in a nested loop, the inner loop will be stop executed.
  •  When few rules are met then it is used to stop the statements.
  • In the switch statement the break key word has a specific role.
  •  Break statement is used to break the cases in the switch statement to stop the execution at case, it wouldn’t execute the next cases subsequently if the controls of the program are transferred to another case.
  • The dominion of the program displaces to the next part of the program by executethe break statement.
  • The break statement transfers program controls to the next statements of the program it can be called as program execution controller.

Syntax

For break statement in Java the syntax is:

Break;

Break Statement Flow Chart

Break Keyword

Break Statements in Loops

Example:

BreakExample.java
//to understand the implementation of break keyword in java programming language
//usage of break keyword in for loop.  
public class BreakEx {  
public static void main (String [] s) {  
    //for loop in usage
for (int a=1; a<=10; a++) {
        if(a==10) {
            //loop is breaked
break.
        }  
        System.out.println(a).
    }  
}  
}  

Output:

1
2
3
4
5
6
7
8
9

Usage of Break Statement in Inner Loops of the java program

Inner loop is breaked or stops execution only if the break statement or keywprd is used in that given loop.

Example:

BreakEx2.java   
public class BreakEx2 {  
public static void main (String [] args) {  
            //outer loop   
for (int m=1;m<=3;m++){    
                    //using inner loop  
for (int n=1;n<=4;n++){    
                        if(m==2&&n==2) {
                            //inside the inner loop break statements is used.
break.
                        }    
                        System.out.println(m+" "+n);
                    }    
            }    
}  
}  

Output:

11
1 2
1 3
14
2 1
3 1
3 2
3 4
4 4

Java Break Statement with Labeled for Loop

The break statement is used with a label for terminating the label created program. The feature is developed since JDK 1.5. So, we can terminate any loop in Java now either it is outer or inner loop.

Example:

BreakEx3.java
//to implement the use of continue statementby implementing the break
//with label inside an inner loop to break outer loop  
public class BreakEx3 {  
public static void main (String [] s) {  
cc:  
for (int m=1;m<=3;m++) {
dd:  
for (int n=1; n<=3;n++) {
                        if(m==2&&n==2) {
                            //using break statement with label  
                            break cc.
                        }    
                        System.out.println(m+" "+n);
                    }    
            }    
}  
}  

Output:

1 1
1 2
1 3
2 1

Java Break Statement in while loop

Example:

BreakWhileEx.java
//Java Program to illustrate the use of break statement  
//uesd in while loop.  
public class BreakWhileEx {
public static void main (String [] s) {  
    //while loop  
    int p=1;
    while(p<=20) {
        if(p==5) {
            //using break statement  
p++;
            break;//it will break the loop  
        }  
        System.out.println(p);
p++;
    }  
}  
}  

Output:

5
6
7
8

Do-while Loop in Java Break Statements

Example:

BreakDoWhileEx.java
//to develop or illustrate the use of break statement through java program
// Java do-while loop inside loop
public class BreakDoWhileEx {
public static void main (String [] s) {  
    //variable declaration
    int q=5;
    //do-while loop  
do {
        if(q==10) {
           //using break statement  
q++;
           break;//it will break the loop  
        }  
        System.out.println(q);
q++;
} while(q<=20);
}  
}  

Output:

5
6
7
8
9
10

Related Topics

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

Java For Loop

A for loop is used to execute a set of statements for a fixed number of times. It takes the following form: for (initialization; condition; update) { statements; } The for loop defines...

2 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

How to create array of objects in Java

Java is an object-oriented programming language therefore everything in Java is based on objects and classes. Array is a data structure that holds data of similar type and dynamically creates...

4 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

The Diamond Operator in Java 7

The Diamond operator is a comparatively recent Java operator originally developed in JDK 7 to enhance type identification and eliminate boilerplate Java code. The Diamond operator is characterized by a...

2 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.