Controlling Program Flow in Scripts

MAXScript provides commands that allow you to control the path that the software follows it evaluates your script. The two most useful groups of commands are the conditional statements and loop statements.

Conditional Statements

Conditional statements simply tell MAXScript to perform a specified command if a certain condition is met.

For example:

if mybox.height == 10 then mybox.width = 20

This changes the width of your box to 20 if its height is equal to 10. You may have taken note of the double equal sign used in the statement above. This is discussed at the end of this topic.

This "if…then…" statement can be expanded to include an "else " statement.

For example:

if mybox.height == 10 then mybox.width = 20 else mybox.width = 10

This statement tells MAXScript to change the width of your box to 20 if its height is 10, otherwise change the width to 10. The "if", "then" and "else" portions of the statement can all be on separate lines,

For example:

if b.height == 10

then b.width = 20

else b.width = 10

If you are entering these commands into the Listener window, you may notice that the commands are not executed line by line, as is typical of the Listener. This is because the Listener recognizes that the "if" statement requires a "then" statement to be complete. In fact, the Listener does not execute the statement after the "then" statement, because it does not know if you intend to include an "else" statement. For this reason, the Listener does not execute an "if…then…" statement until you have either entered a "then" statement or after the next line you type, following the "if…then…" statement.

The double equal sign in the last statement, "==" tells MAXScript to compare two values. The single "=" sign always signifies assignment. This is common in many standard programming languages, such as C++. There are several different conditional parameters used in MAXScript. They are listed here:

Note: Any time MAXScript evaluates a conditional expression, the result is either true or false. MAXScript temporarily stores the value of this result. In an "if…then" statement, MAXScript uses this result to determine the path to follow in the statement. If the result of the "if" statement is true, MAXScript will evaluate the "then" expression. If the result is false, MAXScript will evaluate the "else" expression, assuming one is specified; otherwise, it will cease evaluating the conditional statement. In some scripts, you might see the words on and off used in place of true and false, respectively. These words are interchangeable and they mean the same thing to MAXScript.


Loops are repetitive operations that tell MAXScript to repeat the execution of a collection of commands. Loops are useful for working with large groups of objects, as you can make changes to many objects with one set of commands. For example, if you wanted to make 50 boxes, you could use a loop to execute the create command 50 times, rather than creating the boxes with 50 separate commands. Loops are also useful for changing the properties of multiple objects.

There are several different types of loop. The first to be considered are for and while loops.

For Loops

The for loop iterates through a range of numbers, time values, or a sequenced collection of values, such as an array or object set.

Use a for loop to copy the box,

For example:

for i = 1 to 5 do


box_copy = copy mybox

box_copy.pos = [i * 50, 0, 0]



Everything inside of the parentheses makes up a "block". All loop statements must be created as a block. This statement executes five times. Each time, a new box is created.

A for statement starts with the word ‘for’. Next, you must define how many times the statement will repeat. In this case, you have used the variable "i" to control the loop. The line, "for i = 1 to 5" tells MAXScript to set the variable "i" to the value 1, execute the contents of the loop statement, then set the variable "i" to 2, execute the loop contents, and so on. The variable "i" is called the index for the loop. Each time the loop executes, the index is incremented by 1 automatically, without requiring any user input. Each repetition of the loop is called an iteration. To make MAXScript increment the index by a value other than 1, insert ‘by x’ after the index statement, where x is the amount to increment the index.

The following statement will set the index to 1, then 3, then 5,

For example:

for i = 1 to 5 by 2 do


box_copy = copy mybox

box_copy.pos = [i * 50, 0, 0]



The "do" keyword tells MAXScript to execute the following block or statement each time the loop repeats. Note that in some loops you might see the word "in" in place of the equal sign, "=". These both mean the same thing in for loops, and can be used interchangeably. Note how the index was used within the loop statement to set the position of each new box. This is a useful way to work with multiple object creation.

For loops can also be used to build arrays and/or access values within an array. In the following example, you use a for loop to build an array called ‘arr’:


Remember that because MAXScript is an expression-based language, you are able to use expressions in variable assignment, as seen above in the assignment of the for loop expression to the variable, arr. Note the ‘collect’ statement in the for loop above. This tells MAXScript to collect the results of each iteration in an array, rather than just calculate them. In this case, you only asked MAXScript to collect the value of the index; however, you can collect iterations of any expression.

Finally, for loops can also perform iterations with the contents of an array. In the following example, you use a for loop to access the values of the array you created in the previous example:


The ‘print’ command is a MAXScript function that displays the specified variable in the Listener window. In this example you used ‘in’ instead of an equal sign; however, they are still both acceptable, even when accessing values within an array.

Note: You can also access the number of elements in an array with the .count property.

For example, the previous example could have been written:

for i = 1 to arr.count do print arr[i]

This will return the same result.

While and Do Loops

While and Do loops are used to repeatedly execute an expression until a test expression evaluates as false. The while and do loop are simple variants of one another.

The syntax is:

do <expr> while <expr> -- do loop

while <expr> do <expr> -- while loop

The following example will return the value of a variable, incrementing the variable by -1 with each iteration.



while x>0 do print (x-=1)













MAXScript automatically returns the final value of the loop to the Listener. In this example, MAXScript is also instructed to return the value at each iteration of the loop; therefore, the final value is returned twice.

Both loops repeat the do<expr> as long as the while<expr> evaluates to the value true. The do form evaluates the do<expr> at least at least once, before evaluating the text expression at the end of the loop. The while form evaluates the test expression at the start of the loop, and might not loop at all.

Both forms return, as their value, the result of the do<expr> from the last successful loop iteration. The while form returns the value undefined if the test expression immediately returns false.

For example:


do print (x-=1) while x>10







while x>10 do print (x-=1)



Next Topic

Local and Global Variables