只要指定条件为真,就执行一个语句块。
for (initialization; test; increment) ...statement
- initialization
必选。一个表达式。该表达式在执行循环前仅被执行一次。
- test
必选。布尔表达式。如果 test 为 true,则执行 statement。如果 test 为 false,则结束循环。
- increment
必选。一个表达式。在每次通过循环的结尾执行该增量表达式。
- statement
可选项。test 为 true 时要执行的语句。可以是复合语句。
当循环将执行已知的次数时,通常使用 for 循环。
示例下面的示例说明了一个 for 循环。
/* i is set to 0 at start, and is incremented by 1 at the end of each iteration. Loop terminates when i is not less than 10 before a loop iteration. */ var myarray = new Array(); for (var i = 0; i < 10; i++) { myarray[i] = i; }要求请参见
参考
for...in 语句while 语句