The Java tag, as its name implies, is marked in the specified place, and the program executes to a specific place to return to the mark, and then perform some action.
Introduce the "tags" first to introduce the break and continue keywords.
Break keyword: refers to stopping the current block of code
continue keyword: refers to stop the code block at a time
Why say "code block" see below to know, break and continue not only can be used in for,while,switch and so on.
Question 1.
When the inner loop I equals 2 o'clock, end the two-layer loop?
// double Loop, outer I, inner layer J for (int i = 0; i < 3; i++) { for (int j = 0; J < 3; J + +) { if (i==2)break; // end a two-tier loop System.out.println (j+ " " +i);} }
The result is that break cannot end a two-tier loop because it can only end the inner loop. This problem if the number of layers increased to three layers may be more incompetent ah!
// double cycle, 1 layer i,2 layer J, 3 layer K for (int i = 0; i < 3; i++) { for (int j = 0; J < 3; J + +) { fo R (int k= 0; k < 3; k++) { if(j==2)break; // End 2-layer J-Loop System.out.println (j+ " " +i+ "" +k); }}}
Question 2.
When 2-layer loop J equals 2 o'clock, stop 1-layer loop and then run
for (int i = 0; i < 3; i++) { for (int j = 0; J < 3; J + +) { if< /c8> (i==2)continue; // Stop the cycle of times I System.out.println (j+ " " +i);} }
The same problem also occurs in continue, sometimes the algorithm is so "disgusting." The same problem can occur in a three-layer cycle.
for (int i = 0; i < 3; i++) { for (int j = 0; J < 3; J + +) { for (int k= 0; k < 3; k++) { if(j==2)continue// Stop the current J-Layer Loop System.out.println (j+ " " +i+ "" +k); }}}
Ask two questions and start introducing "tags".
Format: Tag name: code block
Description: Tag name is the English word feel free to write
This place of code is tested as long as the code block wrapped by "{}" can be used.
Experiment:
//Code 1Lable1: for(inti = 0; I < 3; i++) {lable2: for(intj = 0; J < 3; J + +) { if(i==2) BreakLable1;//Stop Lable1System.out.println (j+ "" +i); }}//Code 2Integer[] a ={1,2,3,4};integer[] B={1,2,3,4};lable1: for(Integer a1:a) {lable2: for(Integer a2:b) {if(a1==1) BreakLable1;//Stop Lable1 }}//Code 3Integer a=1;BooleanC=true;BooleanD=true; Lable1: while(c) {lable2: while(d) {a++; if(a==3) BreakLable1;//Stop Lable1System.out.println (a); }}//Code 4 HighlightsInteger a= 1; label1:{System.out.println (a++); if(a==2) BreakLabel1;//Stop Lable1}
Focus on Code 4, why?
Because this is the charm of the label, you can completely set a lot of layers. You can even write a loop body of your own logic.
Integer a= 1; label1:{ lable2:{ System.out.println (a+ +) ; if Break Label1;} }
The "label" is marked by the use of break and continue keyword can be arbitrary jump and jump out. Thus solving the above two problems.
Reasonable use of break and continue with the "label", you can write a good algorithm to solve the algorithm in the jump and jump. But the flaw is also obvious is "the mentality is very disorderly". Not the use of caution!!!
Break and continue special gameplay "tags"