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> ]



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> returns the expression value as the result of the loop. Note that in releases prior 3ds Max 6, OK was returned.

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


Breaking out of a for loop using an exit construct is implemented internally using try/catches, which are extremely slow. It is recommended to use the optional while test added to for loops in 3ds Max 6 for performance reasons.

See also

For Loop

Skipping Loop Iterations