Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the difference between break and continue in javaScript

伊谢尔伦
Release: 2017-07-19 15:22:54
Original
1294 people have browsed it

The difference between break and continue


for(var i=0;i<10;i++){
  if(i>5){
  break;
  }
}
console.log(i);  ---6
Copy after login

•When i=5 and 10, break will be executed and the loop will exit.


 for(var i=1;i<10;i++){
  if(i>5){
  continue;
  }
  num++;
}
console.log(num);  ---4

var num=0;
for(var i=1;i<10;i++){
  if(i%5==0){
  continue;
  }
  num++;
}
console.log(num); ---8
Copy after login

•When i=5 or i=10, the for loop will continue to be executed according to the value of i and exit the loop

When executing multiple loops

The situation of break


outer:
for(var i=0;i<10;i++){
 inter:
  for(var j=0;j<10;j++){
    if(i>5){
    console.log(i); ----6 
     break outer;
    }
  } 
 }
Copy after login

This is the break to the outermost loop


outer:
for(var i=0;i<10;i++){
 inter:
  for(var j=0;j<10;j++){
    if(i>5){
    console.log(i); ----6,7,8,9 
     break inter;
    }
  } 
 }
Copy after login

This is the time to break into the inner loop. Although it will not jump out for the time being, it will still jump out after executing it 4 times

continue situation


var num=0;
outer:
for(var i=0;i<10;i++){
 inter:
  for(var j=0;j<10;j++){
    if(i>5){
    console.log(i); ----6,7,8,9 
     continue outer;
    }
    num++;  
  } 
 }
 console.log(num);     --- 60
Copy after login

Whenever i is greater than or equal to 5, the continue loop will pop up, so there will be forty times less.


var num=0;
outer:
for(var i=0;i<10;i++){
 inter:
  for(var j=0;j<10;j++){
    if(i>5){
    console.log(i); ----6,7,8,9 
     continue inter;
    }
    num++;  
  } 
 }
 console.log(num);     --- 60
Copy after login

The same principle, the loop will still continue to execute, just 40 times less, because the limit is always the value of i, and it will not be true if i is less than or equal to 5 .

The above is the detailed content of Detailed explanation of the difference between break and continue in javaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!