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
// Java Program to show use of break statement
// inside the For loop
public class GFG {
// Main driver code
public static void main(String[] args)
{
// For loop for iteration
for (int i = 3; i <= 50; i += 3) {
if (i == 39) {
// Using Break keyword to suspend
// loop when i become 39
break;
}
// Printing elements showcasing break usage
System.out.print(i + " ");
}
}
}
Output3 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
// Java Program to illustrate the use of break statement
// inside the While loop
// Importing generic Classes/Files
import java.util.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// While loop for iteration
// Creating and initializing variable
// outside loop
int i = 35;
// Condition check
while (i >= 10) {
if (i == 15) {
// Using Break keyword to suspend
// loop when i become 15
// Decrementing variable initialized above
i--;
// Break statement
break;
}
// Printing elements showcasing break
System.out.print(i + " ");
i--;
}
}
}
Output35 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
// Java Program to illustrate the use of break statement
// Inside the do-While loop
// Importing all generic classes
import java.util.*;
// Importing specific class to take
// user input from the user
import java.util.Scanner;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Using do-While loop
// Creating and initializing a variable
// over which execution occurs
int i = 0;
// do loop consisting of executable statements
do {
if (i == 80) {
// Incrementing variable by 5
i += 5;
// Using Break keyword to
// suspend loop when i become 80
break;
}
System.out.print(i + " ");
// Printing elements to show break usage
i += 5;
}
// Condition check
while (i <= 100);
}
}
Output0 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
// Java Program to illustrate the use of break statement
// inside the Switch Statement
// Importing generic Classes/Files
import java.util.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a variable for switch expression
int numb = 20;
// Switch expression
switch (numb) {
// Case statements
case 10:
System.out.println("TEN");
// Break keyword to suspend the switch case
// if given condition is fulfilled
break;
case 20:
System.out.println("TWENTY");
break;
case 30:
System.out.println("THIRTY");
break;
// Default case statement
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
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
int i=10;
GFG:
{
// first statement of block GFG
System.out.println("Block begin");
// if i==10 then block GFG will stop executing
if(i==10){
break GFG;
}
// this will not execute if i==10
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:Java// Java
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