The for statement lets you repeat a statement or compound statement a specified number of times. The body of a for statement is executed zero or more times until an optional condition becomes false.
for ( init-expression ; cond-expression ; loop-expression ) statement
Execution of a for statement proceeds as follows:
This example illustrates the for statement:
myema[ 0 ] = Close[ 0 ];
for( i = 1; i < BarCount; i++ )
myema[ i ] = 0.1 * Close[ i ] + 0.9 * myema[ i - 1 ];
This example iterates all bars of close array to calculate exponential moving average.For loop is extremely flexible.
loop-expression can be ANY kind of expression you wish. You can produce not only regular series like this:
i = 0; i < BarCount;
i = i + 3 ) //
increment by 3 every iteration
but you can produce exponential series too:
i = 1; i < BarCount;
i = i * 2 ) //
produces series of 1, 2, 4, 8, 16, 32, ...