The infamous Goto programming language starts with
GotoKeywords. In fact
GotoProgram control that originated in assembly language: "If the condition
ASet up, then jump here; If you read the assembly code that is eventually generated by the compiler, you will find that the program contains many jumps. (
JavaThe compiler generates its own "assembler code", but this code is run on
JavaOn the virtual machine, instead of running directly on the
CPUOn the hardware. )
GotoThe statement is a jump at the source level, which makes it incur a bad reputation. If a program always jumps from one place to another, what is the way to identify the program's control flow? Since Edsger Dijkstra published a famous paper
"Goto considered harmful"(
GotoHarmful), the crowd began to denounce
Gotois not even recommended to remove it from the keyword collection. The golden mean is the best way to solve this problem. The real problem is not the use
Goto, but is
GotoAnd, in a few cases,
Gotois also the best way to organize control processes. What is the label even though
GotoStill
JavaA reserved word in the language, but it is not used in languages;
JavaNo
Goto。 However
JavaYou can also perform some actions similar to jump, which
BreakAnd
ContinueThese two keywords are related. They are not actually a jump, but rather a way of interrupting an iterative statement. The reason they are included in
GotoIssues, because they use the same mechanism: tags. The label is followed by a colon-like identifier, as follows:
Label1:
In
Java, the only place where the label works is just before the iteration statement. "Just before" means that it is not good to place any statements between tags and iterations. The only reason to set the label before the iteration is that we want to nest another iteration, or a switch. This is due to
BreakAnd
ContinueKeywords usually just interrupt the current loop, but if used with a tag, they break the loop until the label is in place:
label1:outer-iteration{inner-iteration{//...break;//(1)//...continue;//(2)//...continue label1;//(3)//...break label1;//(4)}}
In (1),
BreakInterrupts an internal iteration and returns to an external iteration. In (2),
ContinueMoves the execution point back to the beginning of the internal iteration. In (3),
Continue Label1Interrupts both internal iterations and external iterations, and goes directly to
Label1Then, it actually continues the iterative process, but begins with an external iteration. In (4),
Break Label1Also interrupts all iterations and returns to the
Label1But does not re-enter the iteration. In other words, it is actually completely aborted by two iterations. Sample source below is a label for
forLoops and
whileExamples of loops:
Package Com.mufeng.thefourthchapter;public class labeled {public static void main (string[] args) {System.out.println (" The label is used for the For Loop example "); int i = 0;outer://Can ' t has statements here cannot have a statement for (; true;) {//infinite loop infinite loop inner://Can ' t has statements here cannot have a statement for (; i <; i++) {System.out.println ("i =" + i); F (i = = 2) {System.out.println ("continue"); continue;} if (i = = 3) {System.out.println ("break"); i++;//Otherwise I never gets incremented otherwise I will not get incremental break;} if (i = = 7) {System.out.println ("continue outer"); i++;//Otherwise I never gets incremented otherwise I will not get incremental continue outer;} if (i = = 8) {System.out.println ("break outer"), Break outer;} for (int k = 0; k < 5; k++) {if (k = = 3) {System.out.println ("continue inner"); Continue inner;}}} System.out.println ("label for example of a while loop"), I = 0;outer:while (true) {System.out.println ("outer while loop"), while (true) {i+ +; System.out.println ("i =" + i), if (i = = 1) {System.out.println ("continue"); continue;} if (i = = 3) {System.out.println ("continue Outer "); continue outer;} if (i = = 5) {System.out.println ("break"); if (i = = 7) {System.out.println ("break outer"), Break Outer;}}}}
Output results
Example of a label for A for loop i = 0continue Inneri = 1continue Inneri = 2continuei = 3breaki = 4continue Inneri = 5continue Inneri = 6contin UE Inneri = 7continue outeri = 8break Outer Label For example of a while loop outer while Loopi = 1continuei = 2i = 3continue Outerouter while Loopi = 4i = 5breakOuter while Loopi = 6i = 7break outer
Source Code Analysis Note that
BreakWill break
forCycle, and on arrival
forThe increment expression does not execute until the end of the loop. Because
BreakThe increment expression was skipped, so the
i==3The case directly to the
IPerforms an increment operation. In
i==7The case,
Continue outerThe statement jumps to the top of the loop and also skips the increment, so this is also
IDirect increment. If not
Break outerStatement, there is no way to jump out of the outer loop from inside the loop. This is due to
Breakitself can only interrupt the inner loop (
ContinueThe same is true). Of course, if you want to exit at the same time as the interrupt loop, simply use a
returnCan. The same rules also apply to
while:
- The general continue will return to the beginning of the most inner loop (top) and continue execution.
- The tagged continue will reach the position of the label and re-enter the loop that is immediately behind that tag.
- A normal break breaks and jumps out of the current loop.
- A tagged break breaks and jumps out of the loop that the label refers to.
The key points to remember are:
JavaThe only reason you need to use a tag is because there is a loop nesting, and you want to get from multiple layers of nesting
BreakOr
Continue。
Break and continue in Java in conjunction with tag designator Interrupt Loop example detailed (with source)