Compare continue and break in JavaScript

Source: Internet
Author: User
Tags terminates

Translator Press: The best is not, but basic knowledge to master.

    • Original: Javascript:continue vs Break-learn the difference between the Continue and break statements.
    • Translator: Fundebug

In order to ensure readability, this paper uses free translation rather than literal translation. In addition, the copyright of this article belongs to the original author, translation is only used for learning.


In this article, we will detail continue and break analyze the similarities and differences between them, and even use some examples that can be run.

continueAnd break are used to control the loop. Let's start by looking at their definition:

    • Continue: Ends the current execution and continues to the next loop.
    • break: Terminates execution of the entire loop.

Note: break it can also be used in switch , this article mainly describes the use in the loop.

Continue

Consider the following code:

For (Ten; i++) {
Console.log (i);
}

We have a simple for loop, the loop executes 10 times, and the value of I increments from 11 to 10. Each loop prints the value of the current I. The results of the implementation are as follows:

1
2
3
4
5
6
7
8
9
10

What should we do if we only want to print an even number? The use continue can be easily done. In each cycle we check whether it is an odd number, and if so, jump out of the loop and continue the next time. If not, the value of I is printed.

For (Ten; i++) {
Continue
Console.log (i);
}

The results of the implementation are as follows:

2
4
6
8
10

Remember that when you use continue a keyword, the loop ends immediately and continue the code is no longer executed.

Break

We use the same loop to do the example:

For (Ten; i++) {
Console.log (i);
}

If we want the value of I to be 6, terminate the entire loop. Then we can use break :

For (Ten; i++) {
Break
Console.log (i);
}

If you execute the above code, the for loop terminates execution at the time I is 6, so 6 is not printed to the console.

1
2
3
4
5

JavaScript too (GUI) Live (Yi), out of the bug you do not know, it is advisable to access the Fundebug online real-time monitoring .

Nesting for loops

It is important to note that break and continue both are valid only for the current loop. If we have nested loops, we should be careful. Consider the following example:

 for (let i = 0; i < 5; i++) {
if (i% 2 = = 0) continue;
for (let j = 0; J < 5; J + +) {
if (j = = 2) break;
console.log ( ' I = ${i}, J = ${j} ');
/span>

Here we have two loops, each of which executes 5 times (0~4). The outer loop skips the current loop and executes the next when I is an even number. In other words, only if I is 1 or 3, the inner loop will be executed.

The inner loop is terminated as long as the value of J is 2. Therefore, J is only 0 and 1.

The final result is as follows:

0
1
0
1
Featured Reviews
    • [Nicu Micleu?anu]: The best strategy is to break and continue do not use. If you use it to prove you're wrong, try a different method.

    • [Brandon Morelli]: I agree that a functional approach is better to avoid using a for loop. However, understanding continue and break similarities and differences are still very important, in the event of any day encountered.


Copyright NOTICE: Please specify the author fundebug and this address:https://blog.fundebug.com/2018/03/05/javascript-continue-vs-break/ 

Compare continue and break in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.