java跳转语句(Java jump statement)
java跳转语句(Java jump statement)
2.3.5 jump statement
In addition to the if statements, switch statements, for statements, and while/do-while statements, Java also supports two other jump statements: the break statement and the continue statement. It is called a jump statement because Java makes the program move from the sequential execution to other parts through the two statements. Also note that there is no goto statement in Java, and the program's jump is implemented by means of break and continue.
1.break statement
In the switch statement, we have contacted the break statement, that is, it causes the program to jump out of the switch statement instead of sequentially executing the program in the following case.
In the loop statement, you use the break statement to jump out of the loop, ignoring any other statement of the loop body and the loop condition test. When a break statement is encountered in the loop, the loop terminates, and the program continues execution from the statement after the loop.
Unlike C and C++, there are no goto statements for arbitrary jumps in Java because the goto statement destroys the readability of the program and affects the compilation optimization. But Java can use break to implement some of the advantages of goto statements. Java defines an extended form of the break statement to handle this situation, that is, a tagged break statement. This form of break statement not only has the normal break statement jump function, but also can explicitly transfer program control to the designated place of the label. It should be emphasized that although this jump sometimes improves the efficiency of the program, it should be avoided. Tagged break statements are in the form of:
Break tags;
Look at the example below and take a closer look at the use of the break statement:
Int x = 0;
EnterLoop:// Tags
While (x <10) {
X&#43;&#43;;
System.out.println (enter loop, initial value of X: &#43; X);
Switch (x) {
Case 0:
System.out.println (enter switch statement, x