break StatementThe break statement in Java is used to immediately terminate a loop or a switch statement. Once executed, control jumps outside the current loop or switch block.
for, while, do-while)switch statementsThe basic syntax of the break statement is simple and does not take any parameters.
// Basic syntax of break statement
break;
// Using break inside a for loop
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
}
}
1
2
3
4
When the value of i becomes 5, the break statement stops the loop immediately.
break carefully to avoid unexpected loop terminationbreak in switch to prevent fall-throughbreak inside a while loopbreak in nested loops