- 本文中概念,代码均出自《Thinking in Java》
代码示范如下: · for循环中应用:
class Test{
public static void print(String s){
System.out.println(s);
}
public static void main(String args[]){
int i = 0;
outer:
for (;true ; ) {
inner:
for (;i < 10 ; i++) {
print("i = " + i);
if (i == 2) {
print("continue");
continue;
}
if (i == 3) {
print("break");
i++;
break;
}
if(i == 7){
print("continue outer");
i++;
continue outer;
}
if (i == 8) {
print("break outer");
break outer;
}
for (int k = 0; k < 5; k++) {
if (k == 3) {
print("continue inner");
continue inner;
}
}
}
}
}
}
/* Output:
i = 0
continue inner
i = 1
continue inner
i = 2
continue
i = 3
break
i = 4
continue inner
i = 5
continue inner
i = 6
continue inner
i = 7
continue outer
i = 8
break outer
*/
· while循环中应用
class Test{
public static void main(String args[]){
int i = 0;
outer:
while (true) {
System.out.println("Outer while loop");
while (true) {
i++;
System.out.println("i = " + i);
if (i == 1) {
System.out.println("continue");
continue;
}
if (i == 3) {
System.out.println("continue outer");
continue outer;
}
if (i == 5) {
System.out.println("break");
break;
}
if (i == 7) {
System.out.println("break outer");
break outer;
}
}
}
}
}
/* Output:
Outer while loop
i = 1
continue
i = 2
i = 3
continue outer
Outer while loop
i = 4
i = 5
break
Outer while loop
i = 6
i = 7
break outer
*/
原文地址:https://www.cnblogs.com/petewell/p/11422249.html
时间: 2024-10-10 22:41:43