如何在 JavaScript 中使用標籤和 continue 語句?


本教程將教你如何在 JavaScript 中使用標籤和 **continue** 語句。在 **ES5** 中,我們使用 **標籤** 和 goto 語句來跳過迭代,但這在 **ES6** 版本的 JavaScript 中不受支援。因此,我們將使用 continue 語句來跳過迴圈迭代或跳過任何單個迭代。

以下是 *標籤* 和 *continue* 語句的基本定義。

  • **標籤** - 它可以是任何字串,用於為程式碼塊命名或標記。

  • **continue** - 用於跳過迴圈的一次迭代。它是 ES6 中 goto 語句的替代版本。

語法

使用者可以按照以下語法使用標籤。

label:
   statement // it can be a loop, block of code, etc.

在單迴圈中使用標籤和 continue 語句

在本節中,我們將學習如何在單迴圈中使用 continue 語句。在單迴圈中使用 continue 語句時,不需要使用標籤,但我們將使用它來演示如何將標籤和“continue”關鍵字一起使用。

語法

使用者應遵循以下語法,使用標籤和 continue 關鍵字以及 while 迴圈。

loop:
while () {
   if ( condition ) {
      continue loop; // to jump over the next iteration of loop, use continue keyword followed by label of the loop.
   }
}

示例 1

在下面的示例中,我們必須使用單個 while 迴圈併為其新增標籤。我們建立了一個字串陣列。如果使用者在陣列中找到“hello”和“world!”字串,我們將繼續該迴圈迭代並跳到下一個迭代。為了跳過下一個迭代,我們將使用 continue 關鍵字,後跟迴圈的標籤。

<html>
<head>
   <title> Using the label with continue keyword. </title>
</head>
<body>
   <h2> Using the label with continue keyword. </h2>
   <h4> If specific string occurs in the array jump to the next iteration. </h4>
   <div id="output"> </div>
   <script>
      let output = document.getElementById("output");
      let array = ["welcome", "to", "the", "hello", "world!", "tutorialsPoint!"];
      let n = array.length;
      let i = -1;
      loop:
      while (i < n - 1) {
         i++;
         if (array[i] == "hello" || array[i] == "world!") {
            continue loop;
         }
         output.innerHTML += array[i] + " ";
      }
   </script>
</body>
</html>

在上面的輸出中,使用者可以看到,如果陣列中出現“hello”和“world!”字串,它將跳到迴圈迭代,並且不會在輸出中列印。

示例 2

您可以嘗試執行以下帶有標籤和 continue 語句的程式碼。

<html>
<body>
   <script>
      document.write("Entering the loop!<br /> ");
      outerloop: // This is the label name
      for (var i = 0; i < 3; i++) {
         document.write("Outerloop: " + i + "<br />");
         for (var j = 0; j < 5; j++) {
            if (j == 3) {
               continue outerloop;
            }
            document.write("Innerloop: " + j + "<br />");
         }
      }
      document.write("Exiting the loop!<br /> ");
   </script>
</body>
</html>

巢狀 for 迴圈中的標籤和 continue 語句

在上一節中,我們學習瞭如何使用帶 continue 關鍵字的標籤。在本節中,我們將學習如何從子迴圈跳過父迴圈或祖先迴圈的下一個迭代。

我們只需要更改並在 continue 關鍵字後附加父迴圈的標籤。

語法

以下是使用帶巢狀迴圈的 continue 語句的語法。

parentLoop:
   for () {
      childLoop:
      for () {
         if (condition) {
            continue parentLoop; // jump over to the next iteration of parent loop
         }
      }
   }

示例 3

在這個例子中,我們使用了兩個巢狀的 for 迴圈。我們有兩個相同的字串,並使用巢狀的 for 迴圈匹配兩個字串的每個字元。如果兩個字串的任何字元匹配,我們將使用 continue 關鍵字後跟父迴圈的標籤,從子迴圈跳過父迴圈的下一個迭代。

<html>
<head>
   <title> Using the label with continue keyword. </title>
</head>
<body>
   <h2> Using the label with continue keyword. </h2>
   <p> We will jump over the parent loop iteration when string character will match. </p>
   <div id="output"> </div>
   <script>
      let output = document.getElementById("output");
      let str1 = "abcd";
      let str2 = "abcd";
      parentLoop:
      for (let i = 0; i < str1.length; i++) {
         childLoop: for (let j = 0; j < str2.length; j++) {
            if (str2[j] == str1[i]) {
                  continue parentLoop;
            } else {
               output.innerHTML += " str1[ " + i + " ] = " + str1[i] + " str2[ " + j + " ] = " + str2[j] + "<br/>";
            }
         }
      }
   </script>
</body>
</html>

使用者可以看到,當兩個字串的任何字元匹配時,我們將跳到父迴圈的下一個迭代,這就是上述輸出如何在螢幕上呈現的方式。

結論

使用者已經學習瞭如何使用帶有“continue”語句的標籤。與 break 語句一樣,我們不能將 continue 用於 switch-case 或程式碼塊內。

更新於:2022年7月14日

瀏覽量:592

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.