Several ways to interrupt Java iterations
This article summarizes the usage of several keywords in Java that interrupt iterations, and return does not say a word about break and continue and how to implement the GOTO keyword in other languages in Java.
- The difference between break and continue
Both have the function of jumping out of the loop, the difference is that the break jumps out of the loop, directly terminates the for or while loop, does not perform the subsequent iterations, and continue jumps out of the loop refers to jump out of this iteration, and then the next iteration.
Goto Introduction
Goto originated from the assembly language Program control, if a is set up, then jump here, otherwise jump there.
Although Goto is a reserved keyword in Java, it is not used in the program, and there is no Goto in Java. However, Java can also do some similar jumps, which is related to the break and continue keywords, they are not jump, but rather a way to interrupt the iteration, it is included in the Goto issue is discussed together, because they all use the same mechanism: tags.
How to implement Goto in Java
Look at the following code, the actual implementation of the following can be found in several ways of breaking the difference:
Public Class interrupt Iteration { Public Static void Main(string[] args) A for(inti =0; I <Ten; i++) { for(intj =0; J <Ten; J + +) {if(J = =5) {Continue; }if(J = =6) {ContinueA }if(i = =2) { Break; }if(i = =8) { BreakA } System.out.println ("i="+ i +", j=", M); } } }}
- Summary
1, break is to interrupt the internal iteration, back to the external iteration.
2. Continue moves the execution point back to the beginning of the internal iteration.
3, continue A is to interrupt both internal and external iterations, go directly to a, and then continue to start the iterative process from an external iteration.
4. Also interrupts all iterations and returns to a, but does not re-enter the iteration. That is, it actually completely terminates the two iterations.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Several ways to interrupt Java iterations