您的位置:首页 > 编程语言 > Java开发

Thinking in JAVA label for ,similar to go to.

2010-08-26 15:43 351 查看
public class LabeledFor {
public static void main(String[] args) {
int i = 0;
outer: // Can’t have statements here
for (; i<10;i++) { // infinite loop
inner: // Can’t have statements here
for (; i < 10; i++) {
out.print("i = " + i);
if (i == 2) {
out.println("continue");
continue;
}
if (i == 3) {
out.println("break");
i++; // Otherwise i never
// gets incremented.
break;
}
if (i == 7) {
out.println("continue outer");
i++; // Otherwise i never
// gets incremented.
continue outer;
}
if (i == 8) {
out.println("break outer");
break outer;
}
for (int k = 0; k < 5; k++) {
if (k == 3) {
out.println("continue inner");
continue inner;
}
}
}
}
// Can’t break or continue to labels here
}
}


Note that break breaks out of the for loop, and that the increment expression doesn’t occur until the end of the pass through the for loop. Since break skips the increment expression, the increment is performed directly in the case of i == 3. The continue outer statement in the case of i == 7 also goes to the top of the loop and also skips the increment, so it too is incremented directly.

continue outer .相当于在最外面那个循环里面调用continue.里面的i值还是要增加的。

continue inner;相当于在里面那个循环里面调用continue;里面的i值也是要增加的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐