Continue experiment
1 Public classTest {2 Static intI =0;3 Public Static voidMain (string[] args) {4 Lable1:5 while(true){6 7 if(i = = 6){8System.out.println ("Jumping from the Lable2 to the Lable1");9 Break;Ten //continue lable2; One } A - Lable2: - while(true){ the if(i = = 5){ -System.out.println ("Will jump from Lable2 to Lable1"); -i++; - ContinueLable1; + } -i++; +}//Labe2 A}//Lable1 at}//Main -}//class
The upper code has two while loops and is nested, the author sets the Lbale1 in the outer while loop, and sets the Lable2 label on the inner while
When the static variable i =5, the execution
Continue Lable1;
Jumps from the inner loop to the outer loop and executes the outer loop from the beginning
Continue precautions for use:
- Loop internal jump
- Direction up
Error Demo: Non-internal jump
Break experiment
Same as cotinue, but continue jumps to the label and continues execution, and break stops moving at the label
1 Lable1:2 while(true){3 System.out.println (i);4i++;5 6 if(i==3){7System.out.println ("i = 10,break to Lable1");8 BreakLable1;9 }Ten}
Results:
1 02 13 24 i = 10,break to Lable1
It turns out that when executed to i==3, the execution
Break Lable1;
Break to the while () at Lable1. Discovery program no longer executing
Summarize
- The same point of continue and break,
- can implement Goto function (continue lable;break lable)
- Internal jump (Continue/break statement inside the Loop)
- Jump up Continue/break after the label must be on the top of the Continue/break statement
Special case: Loop nesting still follows internal jumps and jumps up
2. Different points of ontinue and break
Continue is jump to the label to continue execution
break jump to stop running at the label
Java break and continue implement Goto functionality