// Java code to illustrate
// using label and break
// instead of goto
// file name: Main.java
public class Main {
public static void main(String[] args)
{
// label for outer loop
outer:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 1)
break outer;
System.out.println(" value of j = " + j);
}
} // end of outer loop
} // end of main()
} // end of class Main
输出:
value of j = 0
在Java中使用continue标签
我们也可以使用continue而不是break。例如参见以下程序。
// Java code to illustrate
// using label and continue
// instead of goto
// file name: Main.java
public class Main {
public static void main(String[] args)
{
// label for outer loop
outer:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 1)
continue outer;
System.out.println(" value of j = " + j);
}
} // end of outer loop
} // end of main()
} // end of class Main