Last Updated : 12 Jul, 2025
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 Program to Illustrate break statement
import java.io.*;
class GFG {
public static void main (String[] args) {
//assigning n as integer value
int n = 1;
//passing n to switch
// it will check n and display output accordingly
switch(n) {
case 1:
System.out.println("GFG");
break;
case 2:
System.out.println("Second Case");
break;
default:
System.out.println("default case");
}
}
}
Explanation: In the above example, the switch statement is evaluated with n = 1. The first case case 1 matches and prints "Case 1". The break statement is executed and the program to exit the switch block and skip any remaining cases.
Syntax of break Statementbreak;
The break statements are used in situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop based on some condition.
Working of Break Statement in JavaThe break statement can also be used in a labeled block (also referred to as the "goto" equivalent) to exit from nested blocks.
Using Break to Exit a LoopUsing break, we can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop.
Note: Break, when used inside a set of nested loops, will only break out of the innermost loop.
Example:
// Java program to illustrate using
// break to exit a loop
class BreakLoop {
public static void main(String args[]) {
// Initially loop is set to run from 0-9
for (int i = 0; i < 10; i++) {
// terminate loop when i is 5
if (i == 5)
break;
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
i: 0 i: 1 i: 2 i: 3 i: 4 Loop complete.Break Statement in Labeled Blocks
Java allows using break
with labels to exit from nested blocks. It often referred to as an alternative to the goto
statement.
Syntax:
label_name: {
// Block of Statements break label_name; // Exits the block labeled "label_name" }
Example:
Java
// Java program to illustrate
// break statement in labeled blocks
public class BreakLabel {
public static void main(String[] args) {
boolean t = true;
// label first
first: {
second: {
third: {
System.out.println("Before break statement");
if (t) {
break second; // Exits the second label block
}
System.out.println("This won't execute.");
}
System.out.println("This won't execute.");
}
System.out.println("After second block.");
}
}
}
Before break statement After second block.
Explanation: The program enters the first labeled block. It moves into the second labeled block and then into the third labeled block. When t is true, the break second; exits the second block by skipping all further code within it. The program then prints "After second block."
Summary of Break Statement Usagebreak
statement is used to exit a loop early.goto
.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4