JSL provides the For(), While(), Summation(), and Product() functions to repeat (or iterate) actions according to the conditions that you specify.
Note: A similar function called For Each Row() is for iterating actions over rows of a data table. See If for an example. Additional Ways to Access Data Values in Data Tables also describes iterating through table rows.
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:
Sets the s variable to 0. This variable holds the sum.
|
|
Begins the For() loop.
|
|
Sets the initialization variable (i) to 0. This expression is performed only once.
|
|
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.
|
|
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.
|
|
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.
A related function is While(), which repeatedly tests the condition and evaluates its body script as long as the condition is true. The syntax is:
For example, here are two different programs that use a While() loop to find the least power of 2 that is greater than or equal to x (287). The result of both programs is 512.
Sets x to 287.
|
|
Sets y to 1.
|
|
Begins the While() loop.
|
|
Shows the value of y (512).
|
|
Sets k to 0.
|
|
Begins the While() loop.
|
|
Raises 2 to the exponent power of k and continues evaluating the loop as long as the result is less than 287.
|
|
As with For() loops, While() 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.
Sets the s variable to the value of the function.
|
|
Begins the Summation() loop.
|
|
Sets i to 1.
|
|
Sets the limit of i to 10.
|
|
All values of i from 1 to 10 are added together, resulting in 55.
|
|
This behavior is similar to Σ in the Formula Editor. The following expression:
Summation(i = 1, N Row(), x ^ 2);
The Product() function is similar to Summation() except that it multiplies the body results rather than adding them. The syntax is the same as for Summation(). For example:
In this example, the initial value of i is 1, the upper limit is 5, then all integer values of i up to 5 are multiplied.
The Break() and Continue() functions give you more control over looping. Break() immediately stops the loop and proceeds to the next expression that follows the loop. Continue() is a gentler form of Break(). It immediately stops the current iteration of the loop and continues with the next iteration of the loop.
Break() is typically used inside a conditional expression. For example:
If(i == 3, Break());
Begins the For() loop.
|
|
Sets i to 1.
|
|
As long as i is less than or equal to 5, continues evaluating the loop.
|
|
Begins the If() loop.
|
|
If i is equal to 3, breaks the loop.
|
|
Prints the string "i=" to the log.
|
|
Places "i=" on the same line as the value that follows.
|
|
Prints the value of i to the log.
The For() loop then repeats until the value of i is less than or equal to 5, breaking and printing only when i is less than 3.
|
|
Note that when the If() and Break() expressions follow Print(), the script prints the values of i from 1 to 3, because the loop breaks after "i=3" is printed.
Begins the For() loop.
|
|
Sets i to 1.
|
|
Begins the If() loop.
|
|
Ends the If() loop.
|
|
Prints the string "i=" to the log.
|
|
Places "i=" on the same line as the value that follows.
|
|
Prints the value of i to the log.
The For() loop then repeats until the value of i is less than or equal to 5, continuing and printing only when i is equal to or greater than 3.
|
|