The break statement is used in Java to: break a loop and immediately terminate the loop execution; jump out of the switch statement case block, terminate the execution of the case block and transfer control.
The role of the break statement in Java
In Java, the break statement is a control flow statement. Used to break out of loops or switch statements. Its function is as follows:
Break the loop:
Break out of the switch statement:
Example
<code class="java">for (int i = 0; i < 10; i++) { if (i == 5) { break; } // 其他代码 }</code>
In this example, when i equals 5, the break statement will break out of the for loop and continue executing the code after the loop.
<code class="java">switch (number) { case 1: // 代码块 1 break; case 2: // 代码块 2 break; default: // 其他代码 }</code>
In this example, if number is equal to 1, the break statement will jump out of the case 1 block and continue executing the code after the switch statement.
The above is the detailed content of What is the role of break in java. For more information, please follow other related articles on the PHP Chinese website!