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語句breakcontinue語句一起使用,用于標識breakcontinue語句適用的語句。

We'll talk more about the break and continue statements below.

我們將在下面詳細討論breakcontinue語句。

句法 (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語句終止當前循環, switchlabel語句,并將程序控制權轉移到終止語句之后的語句。

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)一起使用,所以跳過了continuefor主體結尾之間的語句。

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/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390544.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390544.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390544.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

馬約拉納費米子:推動量子計算的“天使粒子”

據《人民日報》報道&#xff0c;以華人科學家為主體的科研團隊找到了正反同體的“天使粒子”——馬約拉納費米子&#xff0c;從而結束了國際物理學界對這一神秘粒子長達80年的漫長追尋。該成果由加利福尼亞大學洛杉磯分校何慶林、王康隆課題組&#xff0c;美國斯坦福大學教授張…

leetcode 1711. 大餐計數

大餐 是指 恰好包含兩道不同餐品 的一餐&#xff0c;其美味程度之和等于 2 的冪。 你可以搭配 任意 兩道餐品做一頓大餐。 給你一個整數數組 deliciousness &#xff0c;其中 deliciousness[i] 是第 i?????????????? 道餐品的美味程度&#xff0c;返回你可以用…

您的第一個簡單的機器學習項目

This article is for those dummies like me, who’ve never tried to know what machine learning was or have left it halfway for the sole reason of being overwhelmed. Follow through every line and stay along. I promise you’d be quite acquainted with giving yo…

eclipse報Access restriction: The type 'BASE64Decoder' is not API處理方法

今天從svn更新代碼之后&#xff0c;由于代碼中使用了BASE64Encoder 更新之后報如下錯誤&#xff1a; Access restriction: The type ‘BASE64Decoder’ is not API (restriction on required library ‘D:\java\jdk1.7.0_45\jre\lib\rt.jar’) 解決其實很簡單&#xff0c;把JR…

【躍遷之路】【451天】程序員高效學習方法論探索系列(實驗階段208-2018.05.02)...

(躍遷之路)專欄 實驗說明 從2017.10.6起&#xff0c;開啟這個系列&#xff0c;目標只有一個&#xff1a;探索新的學習方法&#xff0c;實現躍遷式成長實驗期2年&#xff08;2017.10.06 - 2019.10.06&#xff09;我將以自己為實驗對象。我將開源我的學習方法&#xff0c;方法不斷…

react jest測試_如何使用React測試庫和Jest開始測試React應用

react jest測試Testing is often seen as a tedious process. Its extra code you have to write, and in some cases, to be honest, its not needed. But every developer should know at least the basics of testing. It increases confidence in the products they build,…

面試題 17.10. 主要元素

題目 數組中占比超過一半的元素稱之為主要元素。給你一個 整數 數組&#xff0c;找出其中的主要元素。若沒有&#xff0c;返回 -1 。請設計時間復雜度為 O(N) 、空間復雜度為 O(1) 的解決方案。 示例 1&#xff1a; 輸入&#xff1a;[1,2,5,9,5,9,5,5,5] 輸出&#xff1a;5 …

簡單團隊-爬取豆瓣電影T250-項目進度

本次主要講解一下我們的頁面設計及展示最終效果&#xff1a; 頁面設計主要用到的軟件是&#xff1a;html&#xff0c;css&#xff0c;js&#xff0c; 主要用的編譯器是&#xff1a;sublime&#xff0c;dreamweaver&#xff0c;eclipse&#xff0c;由于每個人使用習慣不一樣&…

鴿子為什么喜歡盤旋_如何為鴿子回避系統設置數據收集

鴿子為什么喜歡盤旋鴿子回避系統 (Pigeon Avoidance System) Disclaimer: You are reading Part 2 that describes the technical setup. Part 1 gave an overview of the Pigeon Avoidance System and Part 3 provides details about the Pigeon Recognition Model.免責聲明&a…

scrum認證費用_如何獲得專業Scrum大師的認證-快速和慢速方式

scrum認證費用A few months ago, I got the Professional Scrum Master Certification (PSM I). 幾個月前&#xff0c;我獲得了專業Scrum Master認證(PSM I)。 This is a trending certification nowadays, because most companies operate with some sort of agile methodolo…

981. 基于時間的鍵值存儲

創建一個基于時間的鍵值存儲類 TimeMap&#xff0c;它支持下面兩個操作&#xff1a; set(string key, string value, int timestamp) 存儲鍵 key、值 value&#xff0c;以及給定的時間戳 timestamp。 get(string key, int timestamp) 返回先前調用 set(key, value, timesta…

前端開發-DOM

文檔對象模型&#xff08;Document Object Model&#xff0c;DOM&#xff09;是一種用于HTML和XML文檔的編程接口。它給文檔提供了一種結構化的表示方法&#xff0c;可以改變文檔的內容和呈現方式。我們最為關心的是&#xff0c;DOM把網頁和腳本以及其他的編程語言聯系了起來。…

css 繪制三角形_解釋CSS形狀:如何使用純CSS繪制圓,三角形等

css 繪制三角形Before we start. If you want more free content but in video format. Dont miss out on my Youtube channel where I publish weekly videos on FrontEnd coding. https://www.youtube.com/user/Weibenfalk----------Are you new to web development and CSS?…

密碼學基本概念(一)

區塊鏈兄弟社區&#xff0c;區塊鏈技術專業問答先行者&#xff0c;中國區塊鏈技術愛好者聚集地 作者&#xff1a;于中陽 來源&#xff1a;區塊鏈兄弟 原文鏈接&#xff1a;http://www.blockchainbrother.com/article/72 著權歸作者所有。商業轉載請聯系作者獲得授權&#xff0c…

JAVA-初步認識-第十三章-多線程(驗證同步函數的鎖)

一. 至于同步函數用的是哪個鎖&#xff0c;我們可以驗證一下&#xff0c;借助原先賣票的例子 對于程序中的num&#xff0c;從100改為400&#xff0c;DOS的結果顯示的始終都是0線程&#xff0c;票號最小都是1。 票號是沒有問題的&#xff0c;因為同步了。 有人針對只出現0線程&a…

追求卓越追求完美規范學習_追求新的黃金比例

追求卓越追求完美規范學習The golden ratio is originally a mathematical term. But art, architecture, and design are inconceivable without this math. Everyone aspires to golden proportions as beautiful and unattainable perfection. By visualizing data, we chal…

leetcode 275. H 指數 II

給定一位研究者論文被引用次數的數組&#xff08;被引用次數是非負整數&#xff09;&#xff0c;數組已經按照 升序排列 。編寫一個方法&#xff0c;計算出研究者的 h 指數。 h 指數的定義: “h 代表“高引用次數”&#xff08;high citations&#xff09;&#xff0c;一名科研…

Node js開發中的那些旮旮角角 第一部

#前戲 上一周是我到現公司來最忙碌的&#xff08;最有意思的&#xff09;一周了&#xff0c;為什么這么說呢&#xff1f;因為項目中需要提供服務端對用戶病人信息的一個匯總并以email的形式分享信息的接口&#xff0c;在幾天的時間里調研處理一套實施方案。我們服務端是Node.js…

文件2. 文件重命名

servlet對本機已存在的文件進行重命名。 .jsp界面 1 <form action"<%basePath %>fileAction" method"get" >2 <table>3 <tr>4 <td>輸入文件路徑</td>5 <td&…