Let's talk about the difference between break and continue.
Excerpt from JavaScript advanced programming
for (Var i=0;i<10;i++) {
if (i>5) {break
;
}
}
Console.log (i); ---6
• When i=5 and 10 are executed to the break and exit the loop
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, follow the value of I, continue with the For loop, and exit the loop
When multiple loops are executed
The condition of the 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 a break to the inner surface of the time, although not jump out for the moment, but 4 times after execution, or jumped out
The situation of continue
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 will pop up to continue the cycle, so, will be less than 40 times.
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 continue to execute, but only less 40 times, because the limit is always the value of I, I is less than or equal to 5 will not be set up.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.