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 命令會終止當前的循環,然后從下一個值繼續運行。
實例:
<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>
結果:
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
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com