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.mzph.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&…