Home > Web Front-end > JS Tutorial > Detailed explanation of the differences between Continue and Break syntax in JS

Detailed explanation of the differences between Continue and Break syntax in JS

青灯夜游
Release: 2021-04-22 09:12:47
forward
1625 people have browsed it

Detailed explanation of the differences between Continue and Break syntax in JS

In this article, we will introduce continue and break in detail, analyze their similarities and differences, and even use some Runnable instance.

continue and break are both used to control loops. Let’s take a look at their definitions first:

  • Continue: End the current execution and continue with the next cycle.
  • Break: Terminate the execution of the entire loop.

Note: break can also be used in switch. This article mainly introduces its use in loops.

Continue

Consider the following code:

for (let i = 1; i <= 10; i++) {
    console.log(i);
}
Copy after login
Copy after login

We have a simple for loop that executes 10 times , the value of i increases from 1 to 10. Each loop prints out the current value of i. The execution result is as follows:

Detailed explanation of the differences between Continue and Break syntax in JS

#What should we do if we only want to print even numbers? It's easy to do using continue. In each loop we check whether it is an odd number, and if so, jump out of the loop and continue to the next one. If not, print the value of i.

for (let i = 1; i <= 10; i++){
  if (i % 2 == 1) continue;
  console.log(i);
}
Copy after login

The execution results are as follows:

Detailed explanation of the differences between Continue and Break syntax in JS

Remember, when using the continue keyword, the loop ends immediately. continueFollowing code will no longer be executed.

Break

Let’s use the same loop for the example:

for (let i = 1; i <= 10; i++) {
    console.log(i);
}
Copy after login
Copy after login

If we want to When the value of i is 6, the entire loop is terminated. Then we can use break:

for (let i = 1; i <= 10; i++) {
    if (i == 6) break;
    console.log(i);
}
Copy after login

If the above code is executed, the for loop will terminate when i is 6, so 6 will not be printed. to the console.

Detailed explanation of the differences between Continue and Break syntax in JS

[Recommended learning: javascript advanced tutorial]

Nested For Loop

It is worth noting that break and continue are only valid for the current loop. If we have nested loops, we have to be careful. Let's take a look at 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}`);
    }
}
Copy after login

Here we have two loops, each of which will be executed 5 times (0~4). When i is an even number, the outer loop skips the current loop and executes the next one. That is to say, the inner loop will be executed only when i is 1 or 3.

The inner loop terminates as long as the value of j is 2. Therefore, j only has 0 and 1.

The final result is as follows:

Detailed explanation of the differences between Continue and Break syntax in JS

English original address: https://codeburst.io/javascript-continue- vs-break-47b5c15cacc6

This article adopts free translation, and the copyright belongs to the original author

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of Detailed explanation of the differences between Continue and Break syntax in JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:fundebug.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template