如何在 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 或代码块内。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP