Executes a block of statements for as long as a specified condition is true.
Syntaxfor ([ initialization ]; [ test ]; [ increment ]) {
statement
}
In the following example, the for statement executes the enclosed statements as follows:
i
is evaluated.i
is less than or equal to 9, the document.write
statements are executed and i
is reevaluated.i
is greater than 9, the condition becomes false and control is transferred outside the loop.
for (var i = 0; i <= 9; i++) {
document.write (i);
document.write (" ");
}
All of the expressions of the for statement are optional. In the following example, the for statements create an infinite loop, and a break statement is used to exit the loop.
var j = 0;
for (;;) {
if (j >= 5) {
break;
}
j++;
document.write (j + " ");
}
Remarks
You usually use a for loop when the loop is to be executed a known number of times. A for loop is useful for iterating over arrays and for performing sequential processing.
The test of a conditional expression occurs before the execution of the loop, so a for statement executes zero or more times.
On any line in a for loop statement block, you can use the break statement to exit the loop, or you can use the continue statement to transfer control to the next iteration of the loop.
See also Other articles AttributionsMicrosoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4