break, continue and label statements

break statement will immediately exit the loop,

var num = 0;
for (var i=1; i < 10; i++) {
if (i % 5 == 0) {
break;
}
num++;
}
alert(num); //4

Forced to continue following the loop statement. And although also continue statement exits the loop immediately, but will exit the loop from the top of the cycle

Ministry to continue.

was num = 0 ;
for ( the i = 1; i <10; i ++ ) {
 if (i% 5 == 0 ) {
 Continue ; 
} 
Num ++ ; 
} 
Alert (num); // 8

 

 

var num = 0;
outermost:
for (var i=0; i < 10; i++) {
for (var j=0; j < 10; j++) {
if (i == 5 && j == 5) {
break outermost;
}
num++;
}
}
alert(num); //55

Adding this tag will result will not only lead to a break statement to exit within the
Ministry for the statement (that is, using a variable j loop), but also withdraw from the outside for the statement (that is, using a loop variable i).

 

var num = 0;
outermost:
for (var i=0; i < 10; i++) {
for (var j=0; j < 10; j++) {
if (i == 5 && j == 5) {
continue outermost;
}
num++;
}
}
alert(num); //95

 continue statement forces continue the cycle - to exit the inner loop, the outer loop.

Guess you like

Origin www.cnblogs.com/tasly/p/11525359.html