Skip to content
C

break

The break statement immediately stops the loop or switch block it's inside, and control jumps to the code right after that loop or switch.


1. What is the break Statement?

The break statement immediately stops the loop or switch block it's inside, and control jumps to the code right after that loop or switch.

2. Why is it used?

Sometimes there's no reason to keep looping once a certain condition is met — like stopping a search the moment the desired item is found, instead of wastefully checking the rest.

3. Real-Life Example

Think of searching for a specific book on a shelf. The moment you find it, you stop searching further, even if there are more books left to check. break does exactly this inside a loop.

4. Syntax

java
for (...) { if (condition) { break; // exits the loop immediately } }

5. Example Program

java
public class BreakDemo { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i == 5) { break; } System.out.println(i); } } }

Output:

1
2
3
4

6. Key Points to Remember

  • break exits only the loop (or switch) it is directly inside, not any outer loops.
  • Using break can improve performance by avoiding unnecessary further iterations.
  • In nested loops, a labeled break can be used to exit an outer loop directly, if needed.