MAXScript provides a special construct for breaking out of a loop prematurely, even though the loop test <expr> is still true. This is often useful to simplify the loop test, but 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 ... collect loop using a with <expr> does not work as with other loops. The result is always the array of body values collected up to the point of exit.