发布日期: 09/18/2023

For Function

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.

Here is the basic syntax for For():

For( initialization, while, iteration, body );

For example, the following script sums the numbers from 0 to 20:

s = 0;
For( i = 0, i <= 20, i++, s += i );

The script works like this:

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).

);

Ends the loop.

Infinite Loops

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 macOS). You can also select Edit > Stop Script. On macOS, Edit > Stop Script is available only when the script is running.

Comparing For Loops in JSL to C and C++

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.

需要更多信息?有问题?从 JMP 用户社区得到解答 (community.jmp.com).