Java tags? Outer, middle, inner
Please don't worry about loops, but my question is about those keywords: external, intermediate and internal They are not declared as instance variables. Why does the IDE let the code compile? I did some searches on Google. Is this the Java tag? Some keyword in Java? Thank you very much
public class LoopTest{ public static void main(String[] args){ int counter = 0; outer: for(int i = 0; i < 3; i++){ middle: for(int j = 0; j < 3; j++){ inner: for(int k = 0; k < 3; k++){{ } if(k - j > 0){ break middle; } counter++; } } } System.out.println(counter); }
}
Solution
Java support tags This is described in this article from Oracle
So basically, you can have a loop with labels. You can use the keywords continue, break, etc. to control the flow of the loop
The following example illustrates how to use a loop with the break keyword When break is called, it terminates the marked statement, that is, the statement after somelabel
someLabel: for (i = 0; i < 100; i++) { for (j = 0; j < 100; j++) { if (i % 20 == 0) { break someLabel; } } }
The continue keyword handles labels in the same way When you call, for example, continue some tags; The external circulation will continue
As per this so question, you can also do the following structure:
BlockSegment: if (conditionIsTrue) { doSomeProcessing (); if (resultOfProcessingIsFalse()) break BlockSegment; otherwiseDoSomeMoreProcessing(); // These lines get skipped if the break statement // above gets executed } // This is where you resume execution after the break anotherStatement();
Personally, I would never recommend labels Conversely, if you rearrange your code so that you don't need labels (such as breaking complex code into smaller functions), I'll find it easier to understand