

my_label: {
if (str && str.length < 10) {
break my_label:
}
str = str.substr(str.length-10);
}
alert(str);
continue与标签
continue仅对循环语句有意义,因此它只能作用于for、for…in、while和do…while这些语句的内部。默认情况下,它表明停止当前循环并跳转到下一次循环迭代开始处运行。
continue后面也可以带一个标签(label),这时表明从循环体内部中止,并继续到标签(label)指示处开始执行,并且这个标签指示的语句必须是包含此continue的循环语句。
例如:
loop:
代码如下:
for (var j = 0; j < 5; j++)
{
if (j == 2) continue loop;
document.write("loop: " + j +);
}
上面例子continue + label体现不出label的特殊作用,其实完全可以把label去掉,效果相同。下面再看一个例子
代码如下:
document.write("Entering the loop!
");
outerloop: // This is the label name
for (var i = 0; i < 3; i++)
{
document.write("Outerloop: " + i + "
");
for (var j = 0; j < 5; j++)
{
if (j == 3){
continue outerloop;
}
document.write("Innerloop: " + j + "
");
}
}
document.write("Exiting the loop!
");
使用continue label直接跳到外层循环才是其意义所在。
