×

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

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.

Program to find the duplicate characters in a string

Problem statement You have given with a string and your task is to find out the repeated characters from the string and print them. If no character is repeated, then you...

2 minutes read.

URLConnection Class

What is the URL? URL stands for Uniform Resource Locator, is used to specify addresses on the World Wide Web. A URL relates to the identification of any resource connected to the web. URL syntax: Protocol://hostname/other_information(files...

6 minutes read.

Logger class in Java

Logging is a crucial component of Java that aids developers in tracking down mistakes. The logging technique is included with the computer language Java. The possibility of collect the log...

7 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

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.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

How to generate random numbers in Java

Random numbers, also known as fake numbers, are actually a part of a very large sequence, so they are called random numbers. In a defined set of numbers, every number...

6 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 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.

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.

Java Custom Exception

Java Custom Exception Java language facilitates us to create our own exceptions. The class intended to throw the Custom Exception has to be derived from the Java Exceptionor RuntimeExceptionclass. The Java...

4 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.