1 Packagejava_test;2 3 Public classLabelwhile {4 5 Public Static voidMain (string[] args) {6 inti = 0;7Outer while(true) {8System.out.println ("Outer while Loop");9 while(true) {Teni++; OneSystem.out.println ("i=" +i); A if(i = = 1) { -System.out.println ("Continue"); - Continue; the } - if(i = = 3) { -SYSTEM.OUT.PRINTLN ("Continue outer"); - Continueouter; + } - if(i = = 5) { +System.out.println ("Break"); A Break; at } - if(i = = 7) { -SYSTEM.OUT.PRINTLN ("Break outer"); - Breakouter; - } - } in } - } to}
Output
1Outer whileLoop2I= 13 Continue4I= 25I= 36 Continueouter7Outer whileLoop8I= 49I= 5Ten Break OneOuter whileLoop AI= 6 -I= 7 - BreakOuter
It is important to remember, the only reason to use labels in Java are when you have nested loops and we want to break or continue throught more of the one nested level.
Use of Java:label (while loop)