break 语句用于跳出循环。
continue 用于跳过循环中的一个迭代。
下面分别介绍一下break语句和continue的用法:
Break
break 语句可用于跳出循环。
break 语句跳出循环后,会继续执行该循环之后的代码。
js代码
for (i=0;i<10;i++)
{
var x;
if (i==3)
break;
console.log( x="The number is" + i);
}
代码输出结果是:
The number is 0
The number is 1
The number is 2
Continue
continue 语句中断循环中的迭代,如果出现了指定的条件,然后继续循环中的下一个迭代。
js代码
for (i=0;i<=10;i++)
{
var x;
if (i==3) continue;
console.log( x="The number is "+i );
}
代码输出结果
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
通过这两个例子的对比更好的说明了Break 和 Continue 语句的不同用法。
本文介绍了JavaScript中break和continue语句的区别与使用方法。通过示例代码详细展示了如何使用break来跳出循环以及如何利用continue来跳过当前迭代并进入下一次循环。
2962

被折叠的 条评论
为什么被折叠?



