Skip to content
CampusEduX

Control Flow · Lesson 33 of 150

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 min read

01What 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.

02Why 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.

03Real-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.

04Syntax

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

05Example 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

06Key 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.