Sambar Server Documentation

CScript For Loops


The for loop works is typically used when the number of iterations of the loop is known before the loop is entered. The syntax of the for loop consists of three parts separated by semicolons. The general form of the for is:
for ( initialization; expression; increment )

The meaning is exactly:
initialization;
while ( expression ) {
}

The part of the for loop is a statement which is run before the loop is entered. This is expression is typically used to initialize of the loop variable. The second part is a conditional expression; the loop is exited when this evaluates false. The third part of the for loop is a statement which is run every time the loop body is completed. This is typically used to increment the loop counter.

The three elements that comprise the for loop construct usually do just one thing each, however any of them can be left blank. A blank first or last statement will mean no initialization or running increment. A blank comparison statement will always be treated as true. This will cause the loop to run indefinitely unless interrupted by some other means. This might be a return or a break statement.

It is also possible to execute multiple statements into a first or third statement block, separating them with commas. This allows a loop to have more than one controlling variable. The example below illustrates the definition of such a loop, with variables hi and lo starting at 100 and 0 respectively and converging.

for (hi = 100, lo = 0; hi >= lo; hi--, lo++)

© 2001 Sambar Technologies. All rights reserved. Terms of Use.