首页 > 代码库 > break 和 continue

break 和 continue

break 结束当前 for,foreach,while,do-while 或者 switch 结构的执行。从循环外代码执行;
break 可以接受一个可选的数字参数来决定跳出几重循环;
 
continue,跳出本次循环,从下次执行;
如:
var i = 0for(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

--

var i = 0for(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

 

 

break 和 continue