Having worked with JavaScript for a long while now, there are times when you will find the need to break out of a loop when a certain condition is met, or just skip the current iteration to the next iteration for one reason or another. These situations can be solved by using the following statements:
使用JavaScript已有很长时间了,有时您会发现满足特定条件时需要跳出循环,或者出于某种原因而将当前迭代跳过到下一个迭代。 通过使用以下语句可以解决这些情况:
- break statement 中断声明
- continue statement 继续声明
These statements can really come in handy to certain logic in the code or especially when wanting to avoid the long way of breaking a loop or skipping an iteration. Let’s see how they can be used.
这些语句确实可以派上用场,可以简化代码中的某些逻辑,尤其是在避免长循环或跳过迭代的漫长过程中。 让我们看看如何使用它们。
违约声明 (Break Statement)
The break
statement is used to terminate a loop and get out of it. Then, the code following the loop code block will be executed next (if any). It is usually used inside a conditional statement whereby when a condition is met, for one reason or another, it stops the loop and gets out of it. How it is done is by simply writing break
.
break
语句用于终止循环并退出循环。 然后,循环代码块之后的代码将被下一步执行(如果有)。 它通常在条件语句中使用,根据条件语句,由于某种原因而满足条件时,它将停止循环并退出循环。 只需编写break
即可完成。
Syntax:
句法:
break;
Example:
例:
for (let i = 0; i < 8; i++) {
if (i === 4) { break; }
console.log("Iteration i: " + i);
}// Output:
Iteration i: 0
Iteration i: 1
Iteration i: 2
Iteration i: 3
Without the break
statement, the output will typically appears as follow:
如果没有break
语句,则输出通常如下所示:
// Output:
Iteration i: 0
Iteration i: 1
Iteration i: 2
Iteration i: 3
Iteration i: 4
Iteration i: 5
Iteration i: 6
Iteration i: 7
So, to recap, based on the example above we can see that when condition is met, the break
statement is run and as the result, it terminates the loop immediately.
因此,回顾一下,根据上面的示例,我们可以看到,当满足条件时,运行break
语句,结果是,该语句立即终止循环。
This statement also can be used in the switch
statements which are like conditional statements. However, for this article, we will only take a look at the use for loops.
该语句也可以在类似于条件语句的switch
语句中使用。 但是,对于本文,我们将只看一下for循环的用法。
继续声明 (Continue Statement)
The continue
statement is used to skip an iteration of the loop.This statement too, can be used in the switch
statements.
continue
语句用于跳过循环的迭代。该语句也可以在switch
语句中使用。
The continue
statement basically breaks one iteration of the loop, if a specified condition is met, and continues with the next iteration of the loop. How it is written is similar to the break
statement.
如果满足指定条件,则continue
语句基本上中断循环的一次迭代,并继续循环的下一次迭代。 它的写法类似于break
语句。
Syntax:
句法:
continue;
Example:
例:
for (let i = 0; i < 8; i++) {
if (i === 4) { continue; }
console.log("Iteration i: " + i);
}// Output:
Iteration i: 0
Iteration i: 1
Iteration i: 2
Iteration i: 3
Iteration i: 5
Iteration i: 6
Iteration i: 7
Using the example above, we can see that the iteration 4 is skipped because we wrote that when it is 4, we will continue to the next iteration. Thus, its turn is skipped from being printed out.
使用上面的示例,我们可以看到迭代4被跳过了,因为我们写道,当迭代4时,我们将继续进行下一个迭代。 因此,它的转弯被跳过而不再被打印出来。
That’s all for the two statements for now. They are just useful basics that might be used often in your code.
到目前为止,这两个语句就这些了。 它们只是可能在您的代码中经常使用的有用的基础知识。
I hope this article is of help to you. If you think that this article is helpful and it can be of help to other people, please share for them to read as well. Your thoughts and comments are also welcome!
希望本文对您有所帮助。 如果您认为这篇文章对您有所帮助,并且对其他人有帮助,请也分享给他们阅读。 也欢迎您的想法和评论!
Thanks for reading~
谢谢阅读〜
翻译自: https://medium.com/@deddytandean/javascript-break-and-continue-statements-94936bf59052