Let's talk about the difference between break and continue
Excerpted from JavaScript Advanced Programming
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 be exited.
for(var i=1;i<10;i++){ if(i>5){ continue; } num++;}console.log(num); ---4var 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 be executed according to the value of i and exit the loop
When multiple loops are executed
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 inside
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 cycle of breaking to the inner surface. Although it will not jump out for the time being, it still jumps out after 4 executions.
The situation of continuing
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); --- 60Whenever i is greater than or equal to 5, it will pop up and continue the loop, so forty times will be missing.
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); --- 60With the same principle, the loop will continue to be executed, but it is missing 40 times, 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 all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.