黑马程序员技术交流社区

标题: break的使用场景 [打印本页]

作者: 刘辰阳    时间: 2015-10-30 13:59
标题: break的使用场景
/*
* A:break的使用场景
        * 只能在switch和循环中
*/
class Demo1_Break {
        public static void main(String[] args) {
                /*outer:for(int y = 1;y<=5;y++){               
                        if(y==3) break;
                        for (int x = 1;x <= 10 ;x++ ) {
                               
                                if (y==2&&x == 4) {
                                        break outer;                                                        //跳出循环
                                }

                                System.out.println("x = " + x);
                        }
                        System.out.println("***********************************");
                }
                System.out.println("-------------------------");
        }*/

        /*for (int x = 1;x <= 10 ;x++ ) {
                               
                if (x == 4) {
                        break break;                                                        //跳出循环
                }

                System.out.println("x = " + x);
        }*/

        /*for(int y = 1;y<=5;y++){               
                        if(y==3) break;
                        for (int x = 1;x <= 10 ;x++ ) {
                               
                                if (x == 4) {
                                        break;                                                        //跳出循环
                                }

                                System.out.println("x = " + x);
                        }
                        System.out.println("***********************************");
                }
                System.out.println("-------------------------");
        }*/

        outer:for(int y = 1;y<=5;y++){               
                        if(y==3) break;
                        for (int x = 1;x <= 10 ;x++ ) {
                               
                                if (x == 4) {
                                        break outer;                        //跳出循环
                                }

                                System.out.println("x = " + x);
                        }
                        System.out.println("***********************************");
                }
                System.out.println("-------------------------");
        }



}

作者: ppaapc    时间: 2015-10-30 20:37
太长不看 嘻嘻
作者: 迷茫不堪的年纪    时间: 2015-10-30 22:32
可以理解为在循环中, 满足条件后, 可以执行跳出
作者: C威    时间: 2015-10-30 22:35
循环语句中和switch语句中




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2