Java Break Statement
🔷 What is the break
statement?
The break
statement in Java is used to exit a loop or switch statement immediately. When the program encounters a break
, it stops the current loop or switch block and jumps to the code that follows it.
🔷 Syntax:
🔷 Where can break
be used?
-
Inside
for
,while
, ordo-while
loops -
Inside a
switch
statement
📘 Example 1: Using break
in a loop
Output:
🔎 Explanation:
As soon as i == 5
, the break
statement stops the loop execution.
📘 Example 2: Using break
in a switch
Output:
✅ Note: Without break
, the code would continue executing all remaining cases (“fall-through”).