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.
continue
And 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:
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:
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.
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:
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