Home >Web Front-end >JS Tutorial >Detailed explanation of the difference between break and continue in javaScript

Detailed explanation of the difference between break and continue in javaScript

伊谢尔伦
伊谢尔伦Original
2017-07-19 15:22:541364browse

The difference between break and continue


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

•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

•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;
    }
  } 
 }

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;
    }
  } 
 }

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

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

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!

Statement:
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