for Statement

Top  Previous  Next

Executes statements as long as the Boolean condition evaluates to true.

Syntax

for ([initializer]; [condition]; [iterator])

statement

Description

If initializer is present, variables are initialized or expression statements are executed once. If present, condition is evaluated and if true, the expressions of the iterator, if any, are evaluated in sequence, and then another iteration are evaluated and another iteration is performed. If the condition is false or missing, control is transferred to the end of the for statement.

Remarks

A break statement can be used within the for statement to transfer control to the end of the for statement, ending iteration of the statement. A continue statement can be used within the for statement to transfer control to the end of the for statement, executing iterator and performing another iteration.

Examples

var limit = 100;

var i;

var j;

Net.write("Prime numbers between 1 and " + limit + ": ");

for(i=1; i < 100; i++)

{

   var isPrime = true;

   for(j=2; j < i ; j++)

          {

            if(i % j == 0)

                      {

                        isPrime = false;

                        break;

                      }

           }

            if(isPrime)

            Net.write(i + "  ");                                  // Returns Prime numbers between 1 and 100: 1  2  3  5  7  11  13  17  19  23  29  31  37  41  43  47  53  59  61  67  71  73  79  83  89  97  

}