Learning MAXScript > Creating Your Own Scripts >

# 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.

Before you go on, let's make sure you have the correct base scene.

In the new MAXScript Editor you opened in the previous step,

ENTER:
    resetMaxFile #noprompt
    mybox = box length:10 width:10 height:10 wirecolor:blue

Then press Ctrl+E to evaluate the script. This will reset the current scene without the "Are You Sure" prompt and will create a new blue box with dimensions 10x10x10 at the origin.

## Conditional Statements

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

Enter the following in the Listener (and not in the newly opened Script Editor):

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

This changes the width of the box we just created to 20 if its height is equal to 10 (which happens to be the case).

You may have taken note of the double equal sign used in the statement above. This is discussed at the end of this topic and denotes the logical operation "Is Equal To".

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 mybox.height == 10
    then mybox.width = 20
    else mybox.width = 10

What is even more fascinating about MAXScript is that since the if...then...else uses three expressions (one condition and two alternative expressions depending on whether the condition is true or false), the above could be written much shorter as

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

In this case, the right-hand side of the assignment is an IF expression which evaluates the condition whether the height of the box is 10 or not and returns a value of 20 if it is and of 10 if it is not. Then the resulting value is assigned to the left-hand side of the assignment expression.

This can be made clearer with some additional parentheses which are optional and simply improve readability:

FOR EXAMPLE
    mybox.width = (if mybox.height == 10 then 20 else 10)

### If...Then Conditions In the Listener

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.

To avoid the waiting for an "else" statement when typing in the Listener, you can use "if...do..." instead. You cannot use "else" with "if...do...", so the Listener will not wait and will execute the expression after "do" as long as the condition expression after the "if" is true.

### Comparison Operators

The double equal sign in the last statement, "==" tells MAXScript to compare two values. The single "=" sign always signifies value assignment.

This is common in many standard programming languages, such as C++.

There are several different conditional operators used in MAXScript. They are listed here:

- == equal to

- != not equal to

- > greater than

- >= greater than or equal to

- < less than

- <= less than or equal to

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

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.

You can use a for loop to copy the box you created.

Let's expand the script we started in the beginning of this lesson:

EXAMPLE SCRIPT
    resetMaxFile #noprompt --reset the scene
    mybox = box length:10 width:10 height:10 wirecolor:blue --new box
    for i = 1 to 5 do --repeat five times, for each iteration do:
    (
    box_copy = copy mybox --create a copy of the original box
    box_copy.pos = [i*20, 0, 0] --place it i*20 units along x
    box_copy.wirecolor = [i*25,i*50,(5-i)*50] --blue-green gradient
    )--end of the for loop

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 copy is created, placed to a new position and its color is changed.

- A for statement starts with the word 'for'.

- Next, you must define how many times the statement will repeat. In this case, we 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 body, then set the variable "i" to 2, execute the loop body, 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 by.

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

FOR EXAMPLE
    resetMaxFile #noprompt --reset the scene
    mybox = box length:10 width:10 height:10 wirecolor:blue --new box
    for i = 1 to 5 by 2 do --repeat with step of 2:
    (
    box_copy = copy mybox --create a copy of the original box
    box_copy.pos = [i*20, 0, 0] --place it i*20 units along x
    box_copy.wirecolor = [i*25,i*50,(5-i)*50] --blue-green gradient
    )--end of the for loop

- 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 by multiplying by an offset value. This is a useful way to work with multiple object creation. The same approach was used for the color, changing the R, G and B components based on the index.

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 as:
    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.

FOR EXAMPLE
    x=10
    while x>0 do print (x-=1)
RETURNS
    9
    8
    7
    6
    5
    4
    3
    2
    1
    0
    0

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
    x=10
    do print (x-=1) while x>10
RETURNS
    9
    undefined
HOWEVER
    x=10
    while x>10 do print (x-=1)
RETURNS
    undefined

Next Topic

Local and Global Variables