for 循环是您在希望创建循环时常会用到的工具。
下面是 for 循环的语法:
for (语句 1; 语句 2; 语句 3)
{
被执行的代码块
}
语句 1 在循环(代码块)开始前执行
语句 2 定义运行循环(代码块)的条件
语句 3 在循环(代码块)已被执行之后执行
案例:
<script>for(i=0;i<=5;i++){document.write("the number is "+ i + "</br>");}</script>
如上所示,语句一在代码块开始前执行,将0赋值给i;语句2定义运行循环的条件,i<=5;语句3再循环已被执行后操作i++;
效果如下:
the number is 0
the number is 1
the number is 2
the number is 3
the number is 4
the number is 5