1 Packagejava_test;2 3 Public classLabeledfor {4 5 Public Static voidMain (String[]args) {6 intI=0;7Outer//cannot has statement here8 for(;true;){9 Inner:Ten for(; i<10;i++){ OneSystem.out.println ("i=" +i); A if(i==2){ -System.out.println ("Continue"); - Continue; the } - if(i==3){ -System.out.println ("Break"); -i++;//Otherwise I never gets incremented + Break; - } + if(i==7){ ASYSTEM.OUT.PRINTLN ("Continue outer"); ati++; - Continueouter; - } - if(i==8){ -SYSTEM.OUT.PRINTLN ("Break outer"); - Breakouter; in } - for(intk=0;k<5;k++){ to if(k==3){ +SYSTEM.OUT.PRINTLN ("Continue inner"); - ContinueInner; the } * } $ }Panax Notoginseng } - //cannot break or continue-labels here the } +}
Output
1i= 02 ContinueInner3I= 14 ContinueInner5I= 26 Continue7I= 38 Break9I= 4Ten ContinueInner OneI= 5 A ContinueInner -I= 6 - ContinueInner theI= 7 - Continueouter -I= 8 - BreakOuter
Usage:
1 Label1:2outer-iteration{3inner-iteration{4 //...5 Break;//(1)6 //...7 Continue;//(2)8 //...9 ContinueLabel1;//(3)Ten //... One BreakLabel1;//(4) A } -}
In (1), the break breaks out of the inner iteration and your end up in the out iteration.
In (2), the continue moves back to the beginning of the inner iteration.
But in (3), the continue Label1 breaks out of the inner iteration and the outer iteration,all the the- Label1. Then it does in fact continue the iteration,but starting at the outer iteration.
In (4), the "break" Label1 also breaks all the The "the" to Label1, but it does not reenter the iteration. It actually does break out of both iterations.
Java:label use (For loop)