文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Thinking in JAVA label for ,similar to go to.

Thinking in JAVA label for ,similar to go to.

时间:2010-08-26  来源:jrvin

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值也是要增加的。

相关阅读 更多 +
排行榜 更多 +
幸运硬币官方正版下载

幸运硬币官方正版下载

休闲益智 下载
宝宝来找茬手机版 v9.86.00.00 安卓版

宝宝来找茬手机版 v9.86.00.00 安卓版

休闲益智 下载
翻滚飞机大战最新版 v1.0.4 安卓版

翻滚飞机大战最新版 v1.0.4 安卓版

飞行射击 下载