JavaScript 循环的Break 和 Continue

JavaScript 的 break 和 continue 语句

有两种特殊的语句可以在循环中使用: break 和 continue。

—————————————————————————–
Break

break 命令用来打断整个循环, 并继续执行循环后面的代码(如果有的话)。

例子

<html>
<body>
<script type=”text/javascript”>
var i=0
for (i=0;i<=10;i++)
{
if (i==3){break}
document.write(“The number is ” + i)
document.write(“<br />”)
}
</script>
</body>
</html>

结果

The number is 0
The number is 1
The number is 2

———————————————————————
Continue

continue命令将打断当前这次循环,而继续执行下一个循环值。

Example

<html>
<body>
<script type=”text/javascript”>
var i=0
for (i=0;i<=10;i++)
{
if (i==3){continue}
document.write(“The number is ” + i)
document.write(“<br />”)
}
</script>
</body>
</html>

Result

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
The number is 10

Leave a comment

Current ye@r *