我们已经看到,while循环有不同变种。本章将介绍另一种流行的循环叫做for循环。 for 循环
for循环是循环最紧凑的形式,并包含有以下三个重要部分组成:
可以把所有的三个部分中的一行用分号隔开。 语法
rush:js;">
for (initialization; test condition; iteration statement){
Statement(s) to be executed if test condition is true
}
例子:
下面的例子说明一个基本的for循环:
rush:js;">
");
for(count = 0; count < 10; count++){
document.write("Current Count : " + count );
document.write("
"); } document.write("Loop stopped!"); //-->
"); } document.write("Loop stopped!"); //-->
这将产生以下结果,它类似于while循环:
rush:js;">
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped!