如何打破JavaScript中的嵌套循环?

JavaScript is an interpreted scripting language that initially worked inside browsers but later expanded itself to work at the backend and various other purposes. Many of the programming and scripting languages have taken their syntax roots from the C family and JavaScript is no different. The loop in JavaScript and many other languages are similar in syntax and functionality to C/C++.

JavaScript是一种解释型脚本语言,最初可在浏览器中使用,但后来扩展自身以在后端和其他各种目的下使用。 许多编程和脚本语言的语法源于C系列,JavaScript也不例外。 JavaScript和许多其他语言中的循环在语法和功能上与C / C ++类似。

Working with loops is quite straightforward, just set the index, match the iteration condition and finally increment the index to make sure it is exhaustive at any iteration that occurs for which the match condition is false. Here's an example of a for loop.

使用循环非常简单,只需设置索引,匹配迭代条件,最后递增索引,以确保在匹配条件为false的任何迭代中穷举索引。 这是一个for循环的例子。

Code - JavaScript

代码-JavaScript

for(var index = 1; index <= 10; index++) {
	console.log(index);
}

This will print 1 to 10 in the console.

这将在控制台中打印1至10。

But sometimes there are conditions where we don't want the loop to execute further iterations. For example, we when are finding an element in an array and we get it at the first iteration... therefore making other iterations worthless. JavaScript has a break keyword for that. Whenever it encounters that break statement, it exits the loop. Here's an abstract of how you can use break.

但是有时在某些情况下,我们不希望循环执行进一步的迭代。 例如,当我们在数组中查找元素时,我们会在第一次迭代时获得它,因此使其他迭代变得毫无价值。 JavaScript为此具有一个break关键字。 每当遇到该break语句时,它就会退出循环。 这是有关如何使用break的摘要。

Code - JavaScript

代码-JavaScript

for(var index = 1; index <= 10; index++) {
	if( index === 3 ) {
		console.log("Element Found!");
		break;
	}
}

Now, this loop will execute only 3 times because, at the third time, it will encounter the break statement. But note that break statement stops the execution of all the loops. When this is inside the nested loops, it will exit all the loops and execute the statements below the outer most loop. But what if you want to break only in the inside loop? Well... JavaScript has labels for that, just like C has.

现在,此循环将仅执行3次,因为在第三次遇到中断语句。 但是请注意,break语句会停止所有循环的执行。 当它在嵌套循环内时,它将退出所有循环并在最外面的循环下面执行语句。 但是,如果您只想在内部循环中中断怎么办? 嗯... JavaScript带有标签,就像C一样。

Code - JavaScript

代码-JavaScript

index:
for(var index = 1; index <= 3; index++) {
	counter:
	for(var counter = 1; counter <= 5; counter++)
		if( counter % index === 0 && index % counter === 0) {
			console.log("Double Multiple Found!");
			break counter;
		}
}

The index: and counter: are labels. It is like giving a name to a particular control flow. But unlike C family, JavaScript doesn't have goto statement so these labels aren't used only with break and continue. Here are we defining these labels before these two loops. Inside the nested loop, we check if any two numbers are multiples of each other, then we simply break the inner loop and iteration is continued from the next iteration of the outer loop.

索引:和计数器:是标签。 这就像给特定的控制流起一个名字。 但是与C系列不同,JavaScript没有goto语句,因此这些标签不仅仅与break和continue一起使用 。 这是我们在这两个循环之前定义这些标签。 在嵌套循环内,我们检查两个数字是否彼此成倍,然后简单地中断内部循环,并从外部循环的下一次迭代继续进行迭代。

So, this is how you break inside nested loops. It can also be used to improve the efficiency of the program, by not letting the engine to execute the entire code when it's worthless.

因此,这就是您在嵌套循环内的方法。 当不值钱的时候,不让引擎执行整个代码,它也可以用来提高程序的效率。

Hope you like this article. Please like and share your thoughts in the comments below.

希望您喜欢这篇文章。 请在下面的评论中喜欢并分享您的想法。

翻译自: https://www.includehelp.com/code-snippets/how-to-break-in-nested-loops-in-javascript.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值