标签声明 (Label Statement)
The Label Statement is used with the break
and continue
statements and serves to identify the statement to which the break
and continue
statements apply.
Label语句与break
和continue
语句一起使用,用于标识break
和continue
语句适用的语句。
We'll talk more about the break
and continue
statements below.
我们将在下面详细讨论break
和continue
语句。
句法 (Syntax)
labelname:statements
用法 (Usage)
Without the use of a labeled
statement the break
statement can only break out of a loop or a switch
statement. Using a labeled
statement allows break
to jump out of any code block.
如果不使用带labeled
语句,则break
语句只能脱离循环或switch
语句。 使用带labeled
语句可以使break
跳出任何代码块。
例 (Example)
foo: {console.log("This prints:");break foo;console.log("This will never print.");
}
console.log("Because execution jumps to here!")
/* output
This prints:
Because execution jumps to here! */
When used with a continue
statement the labeled
statement allows you to skip a loop iteration, the advantage comes from being able to jump out from an inner loop to an outer one when you have nested loop statements. Without the use of a labeled
statement you could only jump out of the existing loop iteration to the next iteration of the same loop.
当与continue
语句一起使用时,带labeled
语句可让您跳过循环迭代,其优势在于,当您嵌套了循环语句时,能够从内部循环跳到外部循环。 如果不使用带labeled
语句,则只能从现有循环迭代跳出next iteration of the same loop.
的next iteration of the same loop.
例 (Example)
// without labeled statement, when j==i inner loop jumps to next iteration
function test() {for (var i = 0; i < 3; i++) {console.log("i=" + i);for (var j = 0; j < 3; j++) {if (j === i) {continue;}console.log("j=" + j);}}
}/* output
i=0 (note j=0 is missing)
j=1
j=2
i=1
j=0 (note j=1 is missing)
j=2
i=2
j=0
j=1 (note j=2 is missing)
*/// using a labeled statement we can jump to the outer (i) loop instead
function test() {outer: for (var i = 0; i < 3; i++) {console.log("i=" + i);for (var j = 0; j < 3; j++) {if (j === i) {continue outer;}console.log("j=" + j);}}
}/*
i=0 (j only logged when less than i)
i=1
j=0
i=2
j=0
j=1
*/
中断声明 (Break statement)
The break statement terminates the current loop, switch
or label
statement and transfers program control to the statement following the terminated statement.
break语句终止当前循环, switch
或label
语句,并将程序控制权转移到终止语句之后的语句。
break;
If the break statement is used in a labeled statement, the syntax is as follows:
如果在带标签的语句中使用break语句,则语法如下:
break labelName;
例子 (Examples)
The following function has a break statement that terminates the while
loop when i is 3, and then returns the value 3 * x.
以下函数有一个break语句,当i为3时终止while
循环,然后返回值3 * x 。
function testBreak(x) {var i = 0;while (i < 6) {if (i == 3) {break;}i += 1;}return i * x;
}
Run Code
运行代码
In the following example, the counter is set up to count from 1 to 99; however, the break statement terminates the loop after 14 counts.
在下面的示例中,计数器设置为从1到99进行计数。 但是, break语句在14个计数后终止循环。
for (var i = 1; i < 100; i++) {if (i == 15) {break;}
}
Run Code
运行代码
继续声明 (Continue statement)
The continue statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.
continue语句终止当前循环或标记循环的当前迭代中的语句执行,并在下一次迭代时继续执行循环。
continue;
If the continue statement is used in a labeled statement, the syntax is as follows:
如果在带标签的语句中使用了continue语句,则语法如下:
continue labelName;
In contrast to the break statement, continue does not terminate the execution of the loop entirely; instead:
与break语句相反, continue不会完全终止循环的执行; 代替:
In a
while
loop, it jumps back to the condition.在
while
循环中,它跳回到条件。In a
for
loop, it jumps to the update expression.在
for
循环中,它跳转到更新表达式。
例子 (Examples)
The following example shows a while
loop that has a continue statement that executes when the value of i is 3. Thus, n takes on the values 1, 3, 7, and 12.
下面的示例显示一个while
循环,该循环具有一个continue语句,当i的值为3时执行该语句。因此, n取值为1、3、7和12。
var i = 0;
var n = 0;while (i < 5) {i++;if (i === 3) {continue;}n += i;console.log (n);
}
Run Code
运行代码
In the following example, a loop iterates from 1 through 9. The statements between continue and the end of the for
body are skipped because of the use of the continue statement together with the expression (i < 5)
.
在下面的示例中,循环从1到9进行迭代。因为将continue语句与表达式(i < 5)
一起使用,所以跳过了continue和for
主体结尾之间的语句。
for (var i = 1; i < 10; i++) {if (i < 5) {continue;}console.log (i);
}
Run Code
运行代码
翻译自: https://www.freecodecamp.org/news/javascript-loops-label-statement-continue-statement-and-break-statement-explained/