如何在 JavaScript 中使用標籤和 break 語句?
本教程將教會我們如何在 JavaScript 中使用 **標籤** 和 **break 語句**。標籤和 break 語句在 JavaScript 中並不新鮮,你們中的許多人可能都熟悉它們。標籤是一個唯一的字串,我們可以用它來為 JavaScript 中的程式碼塊、迴圈、switch 語句等賦予標識。
我們可以將 **break** 關鍵字與 **標籤** 語句一起使用。它可以停止程式碼在中間的執行。
在本教程中,我們將學習如何在迴圈和程式碼塊中使用標籤和 break 語句。
以下是標籤和 break 關鍵字的基本定義。
**標籤** - 它可以是任何字串,用於為程式碼塊命名或標記。
**Break** - 用於終止程式碼塊、迴圈或 switch 語句的執行。
語法
使用者可以按照以下語法使用標籤。
label: statement // it can be loop, block of code, etc.
在迴圈中使用帶 break 關鍵字的標籤語句
在本節中,我們將學習如何在迴圈中使用標籤語句和 break 關鍵字。大多數程式設計師只在迴圈中使用過 break 語句,但可能沒有使用過標籤語句。
如果我們在迴圈中使用 break 語句,它只會中斷其父迴圈(即它所在的迴圈),但如果我們想中斷 break 關鍵字父迴圈的外層迴圈,我們可以在這些情況下使用標籤。
語法
使用者可以按照以下語法,使用標籤和 break 關鍵字從子迴圈中中斷父迴圈。
parentLoop: // label for parent loop for ( ) { childLoop: label for child loop for () { // body for child loop break parentLoop; // breaking parent loop } }
示例 1
在下面的示例中,我們取了兩個字串。我們使用兩個迴圈來匹配並持續匹配字串中的每個字元。如果兩個字串的任何字元相同,我們將使用 break 關鍵字和父迴圈的標籤停止兩個迴圈的執行。簡單來說,我們將學習如何使用標籤和 break 從子迴圈中停止父迴圈的執行。
<html> <head> <title> Using the label with break keyword. </title> </head> <body> <h2> Using the label with break keyword. </h2> <h4> Users can see the flow of for loops and when stops using break keyword.</h4> <div id="output"> </div> <script> let output = document.getElementById("output"); let str1 = "abcd"; let str2 = "cdef"; parentLoop: for (let i = 0; i< str1.length; i++) { childLoop: for (let j = 0; j < str2.length; j++) { if (str2[j] == str1[i]) { break parentLoop; } else { output.innerHTML += " str1[ " + i + " ] = " + str1[i] + " str2[ " + j + " ] = " + str2[j] + "<br/>"; } } } </script> </body> </html>
在上面的輸出中,使用者可以看到,當 str1 和 str2 的第一個字元匹配時,我們從子迴圈中中斷了父迴圈,並且它停止列印字串的字元。在我們的例子中,'c' 是第一個匹配的字元。
示例 2
您可以嘗試執行以下程式碼,以學習如何使用標籤和 break 語句。
<html> <body> <script> document.write("Entering the loop!<br /> "); outerloop: for (var i = 0; i < 5; i++) { document.write("Outerloop: " + i + "<br />"); innerloop: for (var j = 0; j < 5; j++) { if (j > 3) break; // Quit the innermost loop if (i == 2) break innerloop; // Do the same thing if (i == 4) break outerloop; // Quit the outer loop document.write("Innerloop: " + j + " <br />"); } } document.write("Exiting the loop!<br /> "); </script> </body> </html>
在程式碼塊中使用標籤和 break
在本節中,我們將學習如何在程式碼塊中使用標籤和 break 關鍵字。我們可以像在上一節中為迴圈使用標籤一樣,為程式碼塊指定標籤。然後,我們可以停止特定程式碼塊的執行。
語法
使用者可以檢視以下語法,以瞭解如何在程式碼塊中使用標籤和 break 關鍵字。
parentBlock: { // parent block body childBlock: { break childBlock; // this lines will not be executed } break parentBlock; // this lines of code in parent block will not exeuted. }
示例 3
在下面的示例中,我們將使用父程式碼塊和子程式碼塊標籤。我們將使用相應程式碼塊的標籤和 break 關鍵字來停止這兩個程式碼塊的執行。
<html> <body> <h2> Using the label with break keyword. </h2> <p> Stops the execution of childBlock and parentBlock using the break keyword and respective labels. </p> <div id="output"> </div> <script> let output = document.getElementById("output"); parentBlock: { output.innerHTML = "parent Block execution started. <br/>"; childBlock: { output.innerHTML += "Execution of the child block has been started. <br/> "; break childBlock; output.innerHTML += "This will not be executed. <br/>"; } break parentBlock; output.innerHTML += "Parent block execution ended. <br/>" } output.innerHTML += "Outside the parent block. <br/>" </script> </body> </html>
在上面的輸出中,使用者可以看到,由於我們在子程式碼塊和父程式碼塊內部使用 break 關鍵字和標籤,因此它不會完成這兩個程式碼塊的執行,並且控制流會跳到父程式碼塊之外。
結論
我們學習瞭如何使用標籤和 break 關鍵字,並且我們看到了如何使用標籤來中斷除父迴圈之外的任何其他迴圈。此外,我們還了解了如何在程式碼塊中使用標籤。