The 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
public class BreakExample {
public static void main(String[] args) {
// Break in a loop
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Stops the loop when i is 3
}
System.out.println("Iteration: " + i);
}
// Break in a switch statement
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break; // Exits switch block
default:
System.out.println("Invalid day");
}
}
}
Output
Iteration: 1
Iteration: 2
Wednesday
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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The 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.