zoukankan      html  css  js  c++  java
  • break,continue和return的区别

    break

    1. 使用场景:用于各种循环语句中,跳出循环

    2. 作用:

      • 跳出break所在的那层循环(若有多层循环,只能跳出内层)

        public class A{
            public static void main(String args[]){
                int a = 60;
                switch(a){
                    case 60:
                        System.out.println("及格");
                        break;
                    case 80:
                        System.out.println("优秀");
                    case 100:
                        System.out.println("满分");
                    case default:
                        System.out.println("default");
                }
            }
        }
        //1.输出结果:及格
        /*2.如果各个case中没有break,那么在case匹配正确后,仍会输出接下来的语句。
        输出结果(去除break后):
         及格
         优秀
         满分
         default
        */
        
        public class A {
              public static void main(String args[]){
                   for(int i = 1; i < 6; i++){
                       for(int j = 1; j < 6; j++){
                           System.out.print("haha" + " ");
                           break;
                       }
                       System.out.println("第" + i + "次");
                   }
               }
           }
           /*1.输出结果:
               haha 第1次
               haha 第2次
               haha 第3次
               haha 第4次
               haha 第5次
           */
           /*2.如果内层循环中没有break,则内层循环终止,从外层循环再开始
               haha haha haha haha haha 第1次
               haha haha haha haha haha 第2次
               haha haha haha haha haha 第3次
               haha haha haha haha haha 第4次
               haha haha haha haha haha 第5次
            */
        
      • 若要跳出多层循环,则需要配合标签语句使用

        • 标签语句格式(标签名一定要符合Java的命名规则,不能用保留字)-----标签名:----如Outer:

          public class A {
                public static void main(String args[]){
                    Outer:
                     for(int i = 1; i < 6; i++){
                         Inner:
                         for(int j = 1; j < 6; j++){
                             System.out.print("haha" + " ");
                             break Outer;
                         }
                         System.out.println("第" + i + "次");
                     }
                 }
          }
          /*输出结果:
          	haha
          
          成功跳出两层循环
          */
          

    continue

    1. 使用场景:循环语句中

    2. 作用:中断当前循环,进行下次循环。

      public class B {
            public static void main(String args[]){
                 for(int i = 1; i < 6; i++){
                     if(i == 4) continue;
                     System.out.println("第" + i + "次");
                 }
             }
      }
      /*输出结果
          第1次
          第2次
          第3次
          第5次
          
        直接跳过了i=4,进入下一个循环
      */
      

      值得注意的是,在单独的switch语句中无法使用continue(会发生编译错误,也没有意义),但是可以在一个循环语句的switch中使用。直接进行到下一次循环。

      public class B {
            public static void main(String args[]){
                int a = 80
                boolean flag = false;
                 while(flag == false){
                     flag = true;
                      switch(a){
                          case 60:
                              System.out.println("及格");
                          case 80:
                              System.out.println("优秀");
                              continue;
                          case 100:
                              System.out.println("满分");
                          case default:
                              System.out.println("default");
                      }
                 }
            }
      }
      /*输出结果
      	优秀
      	
      	在语句到达case:80时,直接退出当前循环
      */
      

    return

    1. 使用场景:方法内

    2. 作用:退出当前方法

      public class A {
            public static void main(String args[]){
                 for(int i = 1; i < 6; i++){
                     for(int j = 1; j < 6; j++){
                         System.out.print("haha" + " ");
                         if(j == 2) return;
                     }
                     System.out.println("第" + i + "次");
                 }
                System.out.println("end");
             }
      }
      /*输出结果
      	haha
      	
          不执行for循环后的语句,直接跳出当前方法。
      */
      
  • 相关阅读:
    将数据导入带模板EXCEL
    c#.net循环将DataGridView中的数据赋值到Excel中,并设置样式
    c#.net对excel的操作——创建一个excel报表两个sheet就是2个表分别添加内容
    Sharepoint2013 中想要将网站另存为模板步骤
    使用SharePoint 2010 母版页
    命令添加用户到组
    随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中
    大道至简第五章感悟
    Ljava.lang.Object;@ba8a1dc
    字符串最简单的加密与解密
  • 原文地址:https://www.cnblogs.com/EthanWong/p/13190595.html
Copyright © 2011-2022 走看看