The For() function expects four arguments separated by commas. The first three arguments are rules for how many times to repeat the loop, and the fourth is what to do each time the loop is executed.
The basic syntax for For() is as follows:
For( initialization, while, iteration, body );
s = 0;
For( i = 0, i <= 20, i++, s += i );
s = 0; |
Sets the s variable to 0. This variable holds the sum.
|
For( |
Begins the For() loop.
|
i = 0, |
Sets the initialization variable (i) to 0. This expression is performed only once.
|
i <= 20, |
Each time the loop begins, compares i to 20. As long as i is less than or equal to 20, continue evaluating the loop. If i is greater than 20, immediately break out of the loop.
|
i++, |
At the end of the loop, increments i by 1. Note that this step is done after the body of the loop (next line) is evaluated.
|
s += i |
Evaluates the body of the loop. Adds the value of i to s. After the body is finished, i is incremented (previous line).
|
); |
For loops that always evaluate as true create an infinite loop, which never stops. To stop the script, press ESC on Windows (or COMMAND-PERIOD on Macintosh). You can also select Edit > Stop Script. On Macintosh, Edit > Stop Script is available only when the script is running.
The JSL For() loop works just like it does in the C (and C++) programming language, although the punctuation is different.
Tip: If you know C, watch out for the difference between JSL and C in the use of semicolons and commas. In JSL, For() is a function where commas separate arguments and semicolons join expressions. In C, for is a special clause where semicolons separate arguments and commas join them.