標簽聲明 (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/