A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://webplatform.github.io/docs/javascript/statements/for below:

for · WebPlatform Docs

for Summary

Executes a block of statements for as long as a specified condition is true.

Syntax
for ([ initialization ]; [ test ]; [ increment ]) {
    statement
}
initialization
Optional. An expression. This expression is executed only once, before the loop is executed.
test
Optional. A Boolean expression. If test is true , statement is executed. If test if false , the loop is terminated.
increment
Optional. An expression. The increment expression is executed at the end of every pass through the loop.
statement
Optional. One or more statements to be executed if test is true. Can be a compound statement.
Examples

In the following example, the for statement executes the enclosed statements as follows:


 
 
 
 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 Attributions

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