The break keyword in Java is used to terminate the execution of a loop or switch statement prematurely. When a break statement is encountered, control is transferred to the statement immediately following the enclosing loop or switch.
Usage
The break statement is commonly used in the following scenarios:
To exit a loop (e.g., for, while, do-while) before it has iterated over all elements.
To terminate a switch statement once a matching case has been executed.
publicclassBreakInForLoop{publicstaticvoidmain(String[] args){for(int i =0; i <10; i++){if(i ==5){break;// Terminate loop when i is 5}
System.out.println("i: "+ i);}}}제공
In this example, the for loop iterates from 0 to 9. However, when i equals 5, the break statement is executed, terminating the loop early.
Example 2: Using break in a while Loop
publicclassBreakInWhileLoop{publicstaticvoidmain(String[] args){
int i =0;while(i <10){if(i ==5){break;// Terminate loop when i is 5}
System.out.println("i: "+ i);
i++;}}}제공
Here, the while loop continues as long as i is less than 10. When i reaches 5, the break statement terminates the loop.
Example 3: Using break in a switch Statement
publicclassBreakInSwitch{publicstaticvoidmain(String[] args){
int day =3;switch(day){case1:
System.out.println("Monday");break;case2:
System.out.println("Tuesday");break;case3:
System.out.println("Wednesday");break;// Terminate switch after matching casecase4:
System.out.println("Thursday");break;default:
System.out.println("Invalid day");}}}제공
In this example, the switch statement checks the value of day. When day equals 3, "Wednesday" is printed, and the break statement terminates the switch statement, preventing the execution of subsequent cases.
Example 4: Using break in a do-while Loop
publicclassBreakInDoWhileLoop{publicstaticvoidmain(String[] args){
int i =0;do{if(i ==5){break;// Terminate loop when i is 5}
System.out.println("i: "+ i);
i++;}while(i <10);}}제공
In this do-while loop, the loop will continue as long as i is less than 10. When i equals 5, the break statement will terminate the loop early.
Tips and Best Practices
Use Judiciously: While break can make loops and switch statements more efficient, overuse can make code harder to read and maintain.
Nested Loops: In nested loops, a break statement will only terminate the innermost loop. Use labeled break statements if you need to exit multiple levels of nested loops.
outerLoop:for(int i =0; i <5; i++){for(int j =0; j <5; j++){if(i ==2&& j ==2){break outerLoop;// Terminates both loops}
System.out.println("i: "+ i +", j: "+ j);}}제공
Avoid Spaghetti Code: Using break statements excessively can lead to code that is difficult to follow. Always aim for clear and maintainable code.
Switch Cases: Always use break in each case of a switch statement unless you intentionally want to fall through to the next case.
The
breakkeyword in Java is used to terminate the execution of a loop or switch statement prematurely. When abreakstatement is encountered, control is transferred to the statement immediately following the enclosing loop or switch.Usage
The
breakstatement is commonly used in the following scenarios:for,while,do-while) before it has iterated over all elements.switchstatement once a matching case has been executed.Syntax
Examples
Example 1: Using
breakin aforLoopIn this example, the
forloop iterates from 0 to 9. However, wheniequals 5, thebreakstatement is executed, terminating the loop early.Example 2: Using
breakin awhileLoopHere, the
whileloop continues as long asiis less than 10. Whenireaches 5, thebreakstatement terminates the loop.Example 3: Using
breakin aswitchStatementIn this example, the
switchstatement checks the value ofday. Whendayequals 3, "Wednesday" is printed, and thebreakstatement terminates theswitchstatement, preventing the execution of subsequent cases.Example 4: Using
breakin ado-whileLoopIn this
do-whileloop, the loop will continue as long asiis less than 10. Wheniequals 5, thebreakstatement will terminate the loop early.Tips and Best Practices
breakcan make loops and switch statements more efficient, overuse can make code harder to read and maintain.breakstatement will only terminate the innermost loop. Use labeledbreakstatements if you need to exit multiple levels of nested loops.breakstatements excessively can lead to code that is difficult to follow. Always aim for clear and maintainable code.breakin each case of aswitchstatement unless you intentionally want to fall through to the next case.