Javascript 简明教程
JavaScript - Break Statement
JavaScript 中的 break 语句将终止循环或 switch case 语句。当您将 break 语句与循环一起使用时,控制流将跳出循环并继续执行其他代码。
The break statement in JavaScript terminates the loop or switch case statement. When you use the break statement with the loop, the control flow jumps out of the loop and continues to execute the other code.
break 语句也可以在标记语句中使用时跳至标记语句。它是控制 JavaScript 代码执行流的有用工具。
The break statement can also be used to jump a labeled statement when used within that labeled statement. It is a useful tool for controlling the flow of execution in your JavaScript code.
Syntax
JavaScript 中 break 语句的语法如下所示:
The syntax of break statement in JavaScript is as follows −
break;
OR
break [label];
break 语句中的标签是可选的。
The label is optional with a break statement.
Example (break statement with for loop)
在下面的示例中,我们使用 for 循环进行迭代。我们使用“if”语句在循环中添加条件表达式。当“x”的值为 5 时,它将使用 break 语句“中断”循环。
In the example below, we used the for loop to make iterations. We added the conditional expression in the loop using the 'if' statement. When the value of 'x' is 5, it will 'break' the loop using the break statement.
下面的代码仅在输出中打印 1 到 4 的值。
The below code prints only 1 to 4 values in the output.
<html>
<head>
<title> JavaScript - Break statement </title>
</head>
<body>
<p id = "output"> </p>
<script>
const output = document.getElementById("output");
output.innerHTML += "Entering the loop. <br /> ";
for (let x = 1; x < 10; x++) {
if (x == 5) {
break; // breaks out of loop completely
}
output.innerHTML += x + "<br />";
}
output.innerHTML += "Exiting the loop!<br /> ";
</script>
</body>
</html>
Entering the loop.
1
2
3
4
Exiting the loop!
Example (break statement with the while loop)
下面的代码演示了带有“break”语句的 while 循环。在 while 循环中,无论何时 x 的值为 3 或 7,它都将使用“break”语句终止循环。
The code below demonstrates the while loop with the 'break' statement. In the while loop, whenever the value of x is either 3 or 7, it will terminate the loop using the 'break' statement.
在代码中,我们在检查条件之后更新值。因此,它会先打印 3,然后在下一个迭代中中止循环。
In the code, we update the value after checking the condition. So, it will print 3 first and then terminate the loop in the next iteration.
<html>
<head>
<title> JavaScript - Break statement </title>
</head>
<body>
<p id = "output"> </p>
<script>
let output = document.getElementById("output");
var x = 1;
output.innerHTML += "Entering the loop. <br /> ";
while (x < 10) {
if (x == 3 || x == 7) {
break; // breaks out of loop completely
}
x = x + 1;
output.innerHTML += x + "<br />";
}
output.innerHTML += "Exiting the loop!<br /> ";
</script>
</body>
</html>
Entering the loop.
2
3
Exiting the loop!
Break statement with nested loops
在嵌套循环中,你可以使用“break”语句跳出任何一个循环。例如,如果你对父循环使用“break”语句,代码也将中止嵌套循环的所有迭代表现。对嵌套循环使用“break”语句将仅中止嵌套循环。
You can use the 'break' statement to jump out of any loop when you have nested loops. For example, if you use the 'break' statement with the parent loop, the code will also terminate all iterations of the nested loop. Using the 'break' statement with the nested loop will terminate only the nested loop.
Example
在下面的示例中,x 是父循环的循环变量,y 是子循环的循环变量。
In the example below, x is a looping variable for the parent loop, and y is a looping variable for a child loop.
在嵌套循环中,每当 y 变成 3 时,它将终止循环;在外循环中,每当 x 变成 3 时,它将终止循环。你不会在输出中看到 x > 3 或 y > 2。
In the nested loop, whenever y becomes 3, it will break the loop; in the outer loop, whenever x becomes 3, it will break the loop. You won’t see x > 3 or y > 2 in the output.
<html>
<head>
<title> JavaScript - Break statement </title>
</head>
<body>
<p id = "output"> </p>
<script>
const output = document.getElementById("output");
output.innerHTML += "Entering the loop. <br /> ";
for (let x = 1; x < 10; x++) {
for (let y = 1; y < 10; y++) {
if (y == 3) {
break; // breaks inner loop
}
output.innerHTML += x + " " + y + "<br />";
}
if (x == 3) {
break; // break outer loop
}
}
output.innerHTML += "Exiting the loop!<br /> ";
</script>
</body>
</html>
Entering the loop.
1 1
1 2
2 1
2 2
3 1
3 2
Exiting the loop!
Break statement with switch case statement
switch case 语句基于条件表达式,从多个代码块中执行其中一个代码块。在与条件表达式的值匹配一个或多个 case 后,“break”语句中止 switch case 语句。
The switch case statement executes one of the code blocks from multiple based on the conditional expression. The 'break' statement terminates the switch case statement after matching one or more cases with the conditional expression’s value.
Example
在下面的代码中,我们对每个 case 使用“break”语句。此处,变量 p 的值用作 switch case 语句的条件表达式。它与“case 10”匹配。因此,代码将执行该特定代码块,并使用“break”语句中止 switch case 语句。
In the below code, we used the 'break' statement with each case. Here, the value of variable p works as a conditional expression for the switch case statement. It matches with 'case 10'. So, the code will execute that particular code block and terminate the switch case statement using the 'break' statement.
<html>
<head>
<title> JavaScript - Break statement </title>
</head>
<body>
<p id = "output"> </p>
<script>
const output = document.getElementById("output");
var p = 10;
switch (p) {
case 10:
output.innerHTML = "p is 10";
break;
case 20:
output.innerHTML = "p is 20";
break;
case 30:
output.innerHTML = "p is 30";
break;
default:
output.innerHTML = "p is not 10, 20 or 30";
}
</script>
</body>
</html>
p is 10