Loop Exit

MAXScript provides an exit construct for breaking out of for, do, and while loops prematurely, even though the loop test expression is still true. This is often useful to simplify the loop test expression, and still use error or other complex termination tests in the body <expr> of the loop. Its syntax is:

exit [with <expr> ]

Example

while x < y do

(

local delta = x - y

if delta <= 0 then exit -- time to bail

$foo.pos.x = compute_x (foo / delta)

x += 0.1

)

The optional with <expr> lets you specify what the overall value of the loop expression should be if the loop exits prematurely. If you don't specify an exit value, the loop returns the value undefined upon exit.

Exiting a for ... do loop using a with <expr> results in a value of OK.

Exiting a for ... collect loop using a with <expr> results in the array of body values collected up to the point of exit.