Java Break
Java Break Statement :
The break statement in Java is a control flow command that facilitates the immediate termination of loops or switch statements. When encountered, it causes an abrupt exit from the loop or switch block, transferring control to the statement following the loop or the end of the switch structure.
break in Loops:
Within loops like for, while, or do-while, break serves to prematurely terminate the loop's execution when a certain condition is met. Once the break statement is encountered, the loop halts, and the program continues execution at the statement following the loop.
Example with for loop:
Output:
break in switch statements :
In a switch structure, break is used to exit the switch block after the associated code executes. It prevents the execution from continuing to the subsequent case blocks and directly exits the switch statement.
Example with switch statements :
Output:
The break statement is a crucial tool for controlling the flow of programs in Java, providing a means to exit loops and switch statements based on certain conditions, improving code efficiency and readability.