Block-expressions are the basic constructors for structured code. They allow you to group together a sequence of expressions and have them evaluated in sequence as though they were a single expression. The syntax for <block_expr> is:
( <expr> { (; | <new_line>) <expr> } )
That is, a parenthesized sequence of expressions with each <expr> separated by either a line-break or a ';' semicolon.
The compiler allows empty block-expressions. This is useful when incrementally building code to be able to place an empty expression in some path to be filled in later.
When evaluated, empty expressions yield the value undefined.
(
d = centre.x^2 - (p.x^2 + p.y^2)
newz = if d > 0 then sqrt d else p.z
print newz
)
if x < y then (print x; print y; u += 10; print u)
You can use a mixture of line-breaks or semicolons:
(
print x; print y
u += 10
print u; print z
)
Because a block-expression is an <operand>, you can insert a block of code anywhere you can put an <operand>. This is unlike most languages which limit the places you can put blocks of code to only a few. Further, because all constructs are expressions in MAXScript, a block-expression yields a value that is defined to be the last <expr> in the block. This allows you to use a nested sequence of expressions to compute an operand for some operation:
$box.z =
(
d = x^2 - y^2
if d > 0 then sqrt d else z
)
Here, the result of the final if expression is the result of the block which is then assigned.