码迷,mamicode.com
首页 > 编程语言 > 详细

Java中continue和break

时间:2019-08-28 11:06:01      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:tput   ring   sys   system   oop   https   while   put   java   

  • 本文中概念,代码均出自《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
*/

原文:大专栏  Java中continue和break


Java中continue和break

标签:tput   ring   sys   system   oop   https   while   put   java   

原文地址:https://www.cnblogs.com/petewell/p/11422249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!