日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JavaScript循环:标签语句,继续语句和中断语句说明

發布時間:2023/11/29 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaScript循环:标签语句,继续语句和中断语句说明 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

總結

以上是生活随笔為你收集整理的JavaScript循环:标签语句,继续语句和中断语句说明的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。