← Back to Chapters

Java break Statement

? Java break Statement

? Quick Overview

The break statement in Java is used to immediately terminate a loop or a switch statement. Once executed, control jumps outside the current loop or switch block.

? Key Concepts

  • Used inside loops (for, while, do-while)
  • Used inside switch statements
  • Stops further execution of the current block
  • Can be labeled to exit outer loops

? Syntax / Theory

The basic syntax of the break statement is simple and does not take any parameters.

? View Code Example
// Basic syntax of break statement
break;

? Code Example(s)

? View Code Example
// Using break inside a for loop
public class BreakExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i == 5) {
                break;
            }
            System.out.println(i);
        }
    }
}

? Live Output / Explanation

Output

1
2
3
4

When the value of i becomes 5, the break statement stops the loop immediately.

✅ Tips & Best Practices

  • Use break carefully to avoid unexpected loop termination
  • Prefer clear loop conditions instead of excessive breaks
  • Always use break in switch to prevent fall-through

? Try It Yourself

  • Modify the loop to stop at a different number
  • Use break inside a while loop
  • Experiment with labeled break in nested loops