Usage of Break keyword in Java
Last Updated :
22 Mar, 2023
Break keyword is often used inside loops control structures and switch statements. It is used to terminate loops and switch statements in java. When the break keyword is encountered within a loop, the loop is immediately terminated and the program control goes to the next statement following the loop. When the break keyword is used in a nested loop, only the inner loop will get terminated. Even it is used with if statement to terminated when a certain condition is met.
The break keyword has special usage inside the switch statement. Every case in the switch statement is followed by a break keyword, such that whenever the program control jumps to the next case, it wouldn’t execute subsequent cases.
Real-life Illustration:

Consider a person climbing stairs. Now the red line here is depicting the stair on which his/her shoe slips and he/she rolls backs to the base. Remember if he/she slips, he/she will always be landing on the base.
Here in computers in programming language there is a keyword called ‘break’ which will abruptly stop the further execution of all the statements following it defined in that scope.
Syntax:
break keyword along with a semicolon
break;
Flow chart of Break statement

Use of Break Keyword is mostly found in loop concepts:
Types of loops |
Usage of ‘break’ keyword |
Simple loop |
While the goal of insertion, delete, or updation is completed and there itself wants the program to terminate |
Nested loop |
When the user wants to take command from innermost loop to outer loop |
Infinite loop |
When the program enters into an infinite looping state |
Case 1: Break keyword inside FOR loop
In order to show how to use the break keyword within For loop. Considering when the value of ‘i’ becomes 39, the break keyword plays its role and terminate the for a loop. All values of ‘i’ before 39, are displayed in the result.
Java
public class GFG {
public static void main(String[] args)
{
for ( int i = 3 ; i <= 50 ; i += 3 ) {
if (i == 39 ) {
break ;
}
System.out.print(i + " " );
}
}
}
|
Output
3 6 9 12 15 18 21 24 27 30 33 36
Case 2: Break keyword inside WHILE loop
In order to show how to use the break keyword within the While loop. Considering when the value of ‘i’ becomes 15, the break keyword plays its role and terminate the for a loop. All values of ‘i’ greater than 15 till 35, are displayed in the result.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int i = 35 ;
while (i >= 10 ) {
if (i == 15 ) {
i--;
break ;
}
System.out.print(i + " " );
i--;
}
}
}
|
Output
35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Case 3: Break keyword inside DO-WHILE loop
In order to show how to use the break keyword within the Do-while loop. Considering when the value of ‘i’ becomes 80, the break keyword plays its role and terminate the for a loop. All values of i” before 80, are displayed in the result.
Java
import java.util.*;
import java.util.Scanner;
public class GFG {
public static void main(String[] args)
{
int i = 0 ;
do {
if (i == 80 ) {
i += 5 ;
break ;
}
System.out.print(i + " " );
i += 5 ;
}
while (i <= 100 );
}
}
|
Output
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
Case 4: Break keyword inside the Switch statement
In order to show how to use the break keyword within For loop. The break keyword is used at the bottom of every switch case to terminate each statement sequence and prevent the mixing of switch-case statements.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int numb = 20 ;
switch (numb) {
case 10 :
System.out.println( "TEN" );
break ;
case 20 :
System.out.println( "TWENTY" );
break ;
case 30 :
System.out.println( "THIRTY" );
break ;
default :
System.out.println( "INFINITE" );
}
}
}
|
Case 5: Break keyword inside label block
In order to show how to use the break keyword within label block. The break keyword is used in the line from where we need to break the label block using break word followed by label name.
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int i= 10 ;
GFG:
{
System.out.println( "Block begin" );
if (i== 10 ){
break GFG;
}
System.out.println( "Block end" );
}
}
}
|
Similar Reads
Important Keywords in Java
Keywords refer to the reserved set of words of a language. These are used for some predefined actions. abstract: It is a non-access modifier applicable for classes and methods. It is used to achieve abstraction. For more, refer to abstract keyword in javaenum: It is used to define enum in Javainstan
2 min read
Deque offer() method in Java
The offer(E e) method of Deque Interface inserts the specified element into this Deque if it is possible to do so immediately without violating capacity restrictions. This method is preferable to add() method since this method does not throws an exception when the capacity of the container is full s
5 min read
Java Keywords
In Java, keywords are the reserved words that have some predefined meanings and are used by the Java compiler for some internal process or represent some predefined actions. These words cannot be used as identifiers such as variable names, method names, class names, or object names. Now, let us go t
5 min read
Stack pop() Method in Java
The Java.util.Stack.pop() method in Java is used to pop an element from the stack. The element is popped from the top of the stack and is removed from the same.Syntax: STACK.pop() Parameters: The method does not take any parameters.Return Value: This method returns the element present at the top of
2 min read
Array setLong() method in Java
The java.lang.reflect.Array.setLong() is an inbuilt method in Java and is used to set a specified long value to a specified index of a given object array. Syntax: Array.setLong(Object []array, int index, long value) Parameter: array : This is an array of type Object which is to be updated. index : T
3 min read
Array setShort() method in Java
The java.lang.reflect.Array.setShort() is an inbuilt method in Java and is used to set a specified short value to a specified index of a given object array. Syntax: Array.setShort(Object []array,int index, short value) Parameters: This method takes 3 parameters: array: This is an array of type Objec
3 min read
Deque offerLast() method in Java
The offerLast(E e) method of Deque Interface inserts the specified element into the end of the Deque if it is possible to do so immediately without violating capacity restrictions. This method is preferable to add() method since this method does not throws an exception when the capacity of the conta
5 min read
Java Break Statement
The Break Statement in Java is a control flow statement used to terminate loops and switch cases. As soon as the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop. Example: [GFGTABS]
3 min read
Deque offerFirst() method in Java
The offerFirst(E e) method of Deque Interface inserts the specified element into the front of the Deque if it is possible to do so immediately without violating capacity restrictions. This method is preferable to addFirst() method since this method does not throws an exception when the capacity of t
3 min read
Set remove() method in Java with Examples
The java.util.Set.remove(Object O) method is used to remove a particular element from a Set. Syntax: boolean remove(Object O) Parameters: The parameter O is of the type of element maintained by this Set and specifies the element to be removed from the Set. Return Value: This method returns True if t
1 min read