For the latest version of JMP Help, visit JMP.com/help.


Publication date: 04/21/2023

While Function

While() repeatedly tests the condition and evaluates its body script as long as the condition is true. The syntax is:

While( condition, body );

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.

x = 287;
 

// loop 1:

y = 1;
While( y < x, y *= 2 );
Show( y );
 

// loop 2:

k = 0;
While( 2 ^ k < x, k++ );
Show( 2 ^ k );

The scripts work like this:

x = 287;

Sets x to 287.

// loop 1

y = 1;

Sets y to 1.

While(

Begins the While() loop.

y < x,

As long as y is less than x, continues evaluating the loop.

y *= 2

Multiplies 1 by 2 and then assigns the result to y. The loop then repeats while y is less than 287.

);

Ends the loop.

Show(y);

Shows the value of y (512).

// loop 2

k = 0;

Sets k to 0.

While(

Begins the While() loop.

2 ^ k < x,

Raises 2 to the exponent power of k and continues evaluating the loop as long as the result is less than 287.

k++

Increments k to 1. The loop then repeats while 2 ^ k is less than 287.

);

Ends the loop.

Show(2 ^ k);

Shows the value of 2 ^ k (512).

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

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).