Java Break Statement

Last Updated : 12 Jun, 2026

The break statement in Java is a control flow statement used to immediately terminate a loop, switch statement, or labeled block. When a break statement is encountered, the program exits the current block and transfers control to the next statement following that block.

  • Commonly used inside switch statements to stop fall-through.
  • Transfers control to the statement immediately after the loop or switch.
  • Can be used with labels to exit nested blocks or loops.
Java
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");
      }
    }
}

Output
GFG

Explanation: In the above example, the switch statement is evaluated with n = 1. The case 1 block matches and prints "GFG". The break statement then exits the switch block, preventing the execution of any remaining cases.

Syntax

break;

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.  

Break Statement in Java

Working of Break Statement

When the Java compiler encounters a break statement, it immediately exits the current loop, switch block, or labeled block and continues execution with the next statement outside that block.

  • Stops the execution of the current loop.
  • Exits a matched case in a switch statement.
  • Prevents execution of remaining statements in the current block.

Break Statement in Switch

In a switch statement, the break statement prevents execution from continuing into subsequent cases after a match is found.

  • Java checks the matching case.
  • Statements inside that case are executed.
  • break exits the switch block.

Using Break to Exit a Loop

The break statement can terminate a loop as soon as a specified condition becomes true, regardless of the loop condition.

Working

  • Loop starts executing normally.
  • Condition is checked during each iteration.
  • When the break condition is met, the loop terminates immediately.
  • Control moves to the statement after the loop.

Note: Break, when used inside a set of nested loops, will only break out of the innermost loop.  

Using break to exit a loop in java


Example: Java program to illustrate using break to exit a loop

Java
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.");
    }
}

Output
i: 0
i: 1
i: 2
i: 3
i: 4
Loop complete.

Break Statement in Labeled Blocks

Java allows labels to be associated with blocks of code. A labeled break can be used to exit a specific block directly.

Working

  • Execution enters the labeled block.
  • When break labelName is encountered, Java exits that labeled block.
  • Remaining statements inside the block are skipped.
  • Execution continues after the labeled block.

Syntax: 

label_name: {
// Block of statements
break label_name;
}

Example: Java program to illustrate break statement in labeled blocks

Java
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.");
        }
    }
}

Output
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."

Comment