contents   index   previous   next



for

 

The for statement is a special looping statement. It allows for more precise control of the number of times a section of code is executed. The for statement has the following form.

 

for ( initialization; conditional; loop_expression )

{

   statement

}

 

The initialization is performed first, and then the expression is evaluated. If the result is true or if there is no conditional expression, the statement is executed. Then the loop_expression is executed, and the expression is reevaluated, beginning the loop again. If the expression evaluates as false, then the statement is not executed, and the program continues with the next line of code after the statement. For example, the following code displays the numbers from 1 to 10.

 

for(var x=1; x<11; x++)

{

   Screen.write(x);

}

 

None of the statements that appear in the parentheses following the for statement are mandatory, so the above code demonstrating the while statement would be rewritten this way if you preferred to use a for statement:

 

for( ; ThereAreUncalledNamesOnTheList() ; )

{

   var name=GetNameFromTheList();

   SendEmail(name)

}

 

Since we are not keeping track of the number of iterations in the loop, there is no need to have an initialization or loop_expression statement. You can use an empty for statement to create an endless loop:

 

for(;;)

{

   //the code in this  block will repeat forever,

   //unless the program breaks out of the for loop somehow.

}

 


break