zoukankan      html  css  js  c++  java
  • break 与 continue

    break 与 continue区别

     break语句:会中断循环,并继续执行循环之后的代码

      例:

        for(var i =1; i >=0; ++i){

          if(i>10){

            break;

          }

        document.write(i+' ');

        }

         结果:1 2 3 4 5 6 7 8 9 10

      continue语句:代表跳出当次循环,进入下次循环

      例 1:

        for(var i=1;i<=10;i++){
          if(i==3){
            document.write('hello_king');
            continue;
          }
        document.write(i+'&nbsp');
        }

          结果:1 2 hello_king 4 5 6 7 8 9 10

      例 2:   

        i=1;
          while(i<=10){
            if(i==3){
              continue;
              i++;
            }
            document.write(i+'&nbsp');
            i++;
          }

        结果:死循环

       原因:当i==3时,跳出while循环,而此时的i还是等于3,再次进入while循环,i等于3进入if条件,再次跳出while循环,后边同理,所以i的值一直是3,所以结果为死循环。

        改正:

          i=1;
          while(i<=10){
            if(i==3){

              i++;

              continue;
          
      }
            document.write(i+'&nbsp');
            i++;
          }

        将i++放到continue前面,当i==3时,进入if条件,先进行+1,i现在的值为4,continue跳出循环,再次进入while循环,此时i的值为4,不会进入if条件,所以结果为1 2 4 5 6 7 8 9 10。

  • 相关阅读:
    distributed caching for .net applications
    Linux_18/ mysql
    找到一本不错的Linux电子书,附《Linux就该这么学》章节目录。
    LinuxProbe/ 疑问ABC
    Linux_15/ autofs, DNS
    Linux_14/ SAMBA, NFS
    Linux_13/ 虚拟网站主机功能,Vsftpd
    Linux_12/ Apache, SELinux
    Linux_11/ firewalld-config, SSH, bounding
    Linux_10/ iptables & firewalld
  • 原文地址:https://www.cnblogs.com/hebizaiyi/p/11347582.html
Copyright © 2011-2022 走看看