JavaScript break和continue用法详细介绍

2021年3月11日18:02:06 发表评论 808 次浏览

中断声明:break语句用于跳出循环。它可以用来"跳出" switch()语句。它中断循环并在循环后继续执行代码。

例子:

<!DOCTYPE html>
< html >
     < head > 
         < title >
             JavaScript Break statement
         </ title >
     </ head > 
      
     < body style = "text-align:center;" >
         < div >
             < h1 >lsbin</ h1 >
             < h2 >JavaScript Break</ h2 >
         </ div >
          
         < p id = "GFG" ></ p >
  
         < script >
             var content = "";
             var i;
             for (i = 1; i < 1000 ; i++) {
                 if (i === 6) { 
                     break; 
                 }
                 content += "Geeks" + i + "<br>";
             }
             document.getElementById("GFG").innerHTML = content;
         </ script >
     </ body >
</ html >

输出如下:

JavaScript break和continue1

继续声明:Continue语句在循环中"跳过"一个迭代。它中断循环中的迭代, 并继续执行循环中的下一个迭代。

例子:

<!DOCTYPE html>
< html >
     < head > 
         < title >
             JavaScript continue statement
         </ title >
     </ head > 
      
     < body style = "text-align:center;" >
  
         < div >
             < h1 >lsbin</ h1 >
             < h2 >JavaScript continue</ h2 >
         </ div >
          
         < p id = "GFG" ></ p >
  
         < script >
             var content = "";
             var i;
             for (i = 1; i < 7 ; i++) {
                 if (i === 4) { 
                     continue; 
                 }
                 content += "Geeks" + i + "<br>";
             }
             document.getElementById("GFG").innerHTML = content;
         </ script >
     </ center >
</ body >
</ html >

输出如下:

JavaScript break和continue2

JavaScript标签:在JavaScript中, 标签语句被编写为带有标签名称和冒号的语句。

语法如下:

  • 中断语句:它用于跳出没有标签参考的循环或开关, 而带有标签参考则用于跳出任何代码块。
    break labelname; 
  • 继续声明:它曾经跳过有或没有标签引用的一次循环迭代。
    continue labelname;

例子:本示例使用中断标签语句。

<!DOCTYPE html>
< html >
     < head > 
         < title >
             JavaScript continue statement
         </ title >
     </ head > 
       
     < body style = "text-align:center;" >
          
         < div >
             < h1 style = "color:green;" >
                 lsbin
             </ h1 >
              
             < h2 >JavaScript break</ h2 >
         </ div >
          
         < p id = "GFG" ></ p >
          
         <!-- Script to use break label -->
         < script >
             var val = ["Geeks1", "Geeks2", "Geeks3", "Geeks4", "Geeks5"];
             var print = "";
  
             breaklabel: {
                 print += val[0] + "< br >" + val[1] + "< br >"; 
                 break breaklabel;
                 print += val[2] + "< br >"+ val[3] + "< br >" + val[4]; 
             }
  
             document.getElementById("GFG").innerHTML = print;
         </ script >
  
     </ body >
</ html >

输出如下:

JavaScript break和continue3

例子:本示例使用继续标签。

<!DOCTYPE html>
< html >
     < head > 
         < title >
             JavaScript continue label
         </ title >
     </ head > 
      
     < body style = "text-align:center" >
  
         < div >
             < h1 style = "color:green;" >
                 lsbin
             </ h1 >
              
             < h2 >JavaScript continue</ h2 >
         </ div >
          
         < p id = "GFG" ></ p >
          
         <!-- Script to use continue label -->
         < script >
             var val = ["Geeks1", "Geeks2", "Geeks3", "Geeks4"];
             var val1=["Geeks", "For", "Geeks"]
              
             var print = "";
              
             print += val1[0] + "< br >";
             print += val1[1] + "< br >";
             print += val1[2] + "< br >";
              
             continuelabel: {
                 print += val[0] + "< br >"; 
                 print += val[1] + "< br >"; 
                 continue continuelabel;
                 print += val[2] + "< br >"; 
                 print += val[3] + "< br >"; 
             }
  
             document.getElementById("GFG").innerHTML = print;
         </ script >
     </ body >
</ html >

输出如下:

JavaScript break和continue4

例子:本示例说明了不使用任何标签的情况。

<!DOCTYPE html>
< html >
     < head > 
         < title >
             No label in JavaScript
         </ title >
     </ head > 
      
     < body style = "text-align:center;" >
  
         < div >
             < h1 style = "color:green;" >
                 lsbin
             </ h1 >
              
             < h2 >JavaScript No label</ h2 >
         </ div >
          
         < p id = "GFG" ></ p >
          
         < script >
             var val = ["Geeks1", "Geeks2", "Geeks3", "Geeks4"];
             var val1=["Geeks", "For", "Geeks"]
              
             var print = "";
              
             labelloop:{
                 print += val1[0] + "< br >";
                 print += val1[1] + "< br >";
                 print += val1[2] + "< br >";
             }
              
             print+="< br >";
              
             labelloop1: {
                 print += val[0] + "< br >"; 
                 print += val[1] + "< br >"; 
                 print += val[2] + "< br >"; 
                 print += val[3] + "< br >"; 
             }
      
             document.getElementById("GFG").innerHTML
                     = print;
         </ script >
     </ body >
</ html >

输出如下:

JavaScript break和continue5

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: