How Does the break Statement Work in Java?
How Does the break Statement Work in Java?
331
20-Mar-2025
Updated on 28-Mar-2025
Khushi Singh
28-Mar-2025The break statement functions in Java to halt both loops and switch statements before their normal completion. The break statement halts the present loop execution or switch case before moving program execution to the subsequent statement beneath the loop or switch block. The break statement enables programmers to use it within for, while, and do-while loops and switch statements to manage control flow and achieve better efficiency results.
Break in Loops
By placing the break command inside a loop structure, the statement forces an immediate loop termination to prevent further execution cycles. The statement proves beneficial in array value searches and condition detection prior to normal loop termination.
Break in Switch Statements
Awarding a break command in a switch block allows developers to leave particular cases and stop further execution. The execution process would proceed to the following case without interruptions, even after detecting a match.
Example
Output
The execution of the loop example stops at i == 3 with the help of break to prevent the loop from continuing its iterations. The break statement in the switch example protects against the inadvertent execution of the following cases.