Flow Control


In general, the flow of code instructions is executed in a top- down order. "Control Flow Statements" are special statements that change the order of code block execution. It introduces a choice of paths that the code could follow. Code execution is like a car on a one-way street and control flow statement is like an intersection on that street. So the code execution car may turn left, turn right or continue straight. This is a powerful tool for programmers/scripter and a way to reduce code clutter.

A control flow statement varies between scripting languages but have the same purpose.
We can categorize control flow statements by their effect on code execution. The following are the main control flow statement categories:




In this tutorial we will cover loops, condition, and subroutines only.

Subroutines:


Subroutines are a piece of code which is written once and then used many times from various other places in the program. Subroutines are alternatively known as routines, procedures, functions especially if they return results or methods if they belong to classes.
Back when computer memory was very small compared to today, subroutines were used to reduce program size. More frequently subroutines are used to help make a program more structured by isolating an algorithm, controlling access to a hidden data through a class method and adding program modularity where many programmers could be working on a single program through subroutines. We will talk more about subroutines at the third session.

Conditional Statement:


Conditional statement performs different computations or actions depending on whether the conditional expression evaluates to true or false. We will cover the if-else and case-switch statement as example of conditional flow-control.

The if--else construct is common across many programming languages having varying syntax but with the same basic structure. In English a complete sentence requires a noun and a verb likewise if statement requires a noun (an expression that evaluate to true/false) and a verb (an action or instruction to be executed for true situation and optional instructions for the false situation).
http://en.wikipedia.org/wiki/Conditional_(programming)

Here is the structure of if statement:
if ( x > 0)
{
    print x;
}
else
{
    print "x is negative";
}

1. The statement must start with the key phrase if
2. An expression that evaluate to true false like (x > 0)
3. An action for the true situation
4. An optional key phrase else and an action for the false situation.


We also could chain if statement together or nest them within each other to get further control on our script execution. Depending on code specifics chaining or nesting if statement may results in slow code execution.
When a condition could occur only in the absence of another condition it is faster to use if-else-if in your script. For example:
if-else if-else statement:
if (color == red){
    stop();
}
else if (color == green){
    go();
}
else{
    getReady();
}

Here color cannot be red and green at the same time and only when the color is not Red the Green test will take place. Now this will speed code execution only if the sequence of color is Red followed by Green.



The same code could be expressed as below, however this code block is slower than the above could since each "if statement" gets evaluated regardless whether the preceding condition is true.

if-if-else Example

if ( color == red) {
    stop();
}
if ( color == green) {
    go();
}
else {
    getReady();
}



Now let us look at how to express if statement in specific languages:

MEL

vector $myColor = <<255,0,0>>;
vector $redColor = <<255,0,0>>;
vector $greenColor = <<0,255,0>>;

if( $myColor == $redColor)
    print "My color is red\n";
else if ( $myColor == $greenColor)
    print "My color is green\n";
else
    print "My color is neither red nor green\n";



Maxscript

--if statement in max is followed by the key phrase 'do' when there is no else or else if where instead of 'do' we use the key phrase 'then'.

If-do Example:

if x > 0 do
    print "x is positive"

if-else-then example:

myColor = [0,255,128]
redColor = [255,0,0]
greenColor = [0,255,0]

if myColor.x == redColor.x and
   myColor.y == redColor.y and
   myColor.z == redColor.z then
   print "My color is red\n"

else if  myColor.x == greenColor.x and
          myColor.y == greenColor.y and
          myColor.z == greenColor.z then
          print "My color is green\n"

else
    print "My color is neither red nor green\n"



Python


myColor = [0,255,0]
redColor = [255,0,0]
greenColor = [0,255,0]

if   myColor[0] == redColor[0] and
     myColor[1] == redColor[1] and
     myColor[2] == redColor[2] :
     print 'My color is red\n'

elif &nbspmyColor[0] == greenColor[0] and
      myColor[1] == greenColor[1] and
      myColor[2] == greenColor[2]:
      print 'My color is green\n'

else:
      print 'My color is neither red nor green\n'



Lua


myColor = {0,255,128}
redColor = {255,0,0}
greenColor = {0,255,0}

if myColor[1] == redColor[1] and
  myColor[2] == redColor[2] and
  myColor[3] == redColor[3] then
  print ("My color is red\n")

elseif myColor[1] == greenColor[1] and
      myColor[2] == greenColor[2] and
      myColor[3] == greenColor[3] then
      print ("My color is green\n")

else
    print ("My color is neither red nor green\n")

end



Comparison and Logical operators:


Comparison operators are used to test the relation between to value and always have true (positive digit) or false (zero/Null) results while logical operators are used to compare the results of the comparison operators or to reverse their result.
For example, let m = 2 and w = 4 the, following are how logical and comparison operators are used:



Case - Switch Statement:


A switch-case statement (choice statement) executes one of several code segment based on the controlling conditional value which can be an int or string in MEL, and any value in maxscript. When the control value is equal to case statement value, then the code segment under it gets executed. Sometime, we use case-switch instead of 'if - else if' statement to speed and organize our scripts.

Python and Lua don't have a C-style switch statement. However there are ways to emulate the same effect. In Python an if ... elif ... elif ... sequence is a substitute for the switch or case statements. The following link http://lua-users.org/wiki/SwitchStatement. discusses an alternative way to mimic the switch statement.

MEL

string $myChannel = "TLC";
switch(tolower($myChannel))
{
    case "tlc":
        print "The Learning Channel\n";
        break;
    case "abc":
        print "The American Broadcasting Company Channel\n";
        break;
    case "nbc":
        print "The National Broadcasting Company Channel\n";
        break;
    default:
        print "Unknown channel\n";
}



Maxscript


myChannel = "ABC"
case tolower(myChannel) of
(
    "tlc": print "The Learning Channel\n"
    "abc": print "The American Broadcasting Company Channel\n"
    "nbc": print "The National Broadcasting Company Channel\n"
    default: print "Unknown channel\n"
)