JavaScript-5循环
For循环:
例一:
<html>
<body>
<script type="text/javascript">
for (i = 0; i <= 5; i++)
{
document.write("The number is " + i)
document.write("<br />")
}
</script>
</body>
</html>
实图:
例二:
<html>
<body>
<script type="text/javascript">
for (i = 1; i <= 6; i++)
{
document.write("<h" + i + ">标题 " + i+"</h" + i + ">")
}
</script>
</body>
</html>
实图:
While循环:
例一:
<html>
<body>
<script type="text/javascript">
i = 0
while (i <= 5)
{
document.write("The number is " + i)
document.write("<br>")
i++
}
</script>
</body>
</html>
实图:同for例一。
Do while 循环:(先执行,在判断)
<html>
<body>
<script type="text/javascript">
var i=0
do
{
document.write("The number is " + i)
document.write("<br />")
i=i+1
}
while (i<0)
</script>
</body>
</html>
实图:
Break 语句:break终止循环的运行,然后执行循环之后的代码
<html>
<body>
<script type="text/javascript">
var i=0
for (i=0;i<=10;i++)
{
if (i==3){break}
document.write(i+" ")
}
</script>
</body>
</html>
结果为:0 1 2 。
Continue语句:终止当前循环,然后从下一个值继续运行。
<html>
<body>
<script type="text/javascript">
var i=0
for (i=0;i<=10;i++)
{
if (i==3){continue}
document.write(i+" ")
}
</script>
</body>
</html>
结果为:0 1 2 4 5 6 7 8 9 10