Control Structures Within Script Blocks
A script block by itself is a type of control structure. Triggered by an action statement (IF <condition is true> ...) a set of statements will execute in order as written. After a single run-through, the action statement will be checked again and either transfer to another state, repeat the script block if the conditional is true, or remain in steady state if it is false.
Within a script block, the following functions may be used:
Returns the nth parameter of a set, based on a variable index value. Parameters can be expressions.
Executes a set of statements at least once, repeating so long as a conditional parameter remains true. The conditional is checked after the statements run once.
Executes a group of statements as a single entity in structures that would otherwise allow only one statement to be executed such as Case or IfElse.
IfElse, ?:
Executes one of two parameters based upon a condition parameter. You can use either the IfElse function or the ?: operator, but as a matter of style choose between them as follows:
To control flow, use IfElse:
IfElse(Tired, Sleep(); { Else } Work(); );
To return a value or expression conditionally, use the ?: operator:
Mood = IsHungry ? "Sad" : "Happy";
Conditionally Execute Statements. This statement executes a statement if the condition parameter is true.
Executes a set of statements zero or more times, repeating so long as a conditional parameter remains true. The condition is checked before the statements are executed.
Note that there is a significant difference between the following two control structures: Given X, initialized to zero.
If X < 10;
[
X++;
]
versus
If X < 10;
[
WhileLoop(X < 10, X++; );
]
In the first instance, the If statement will be re-evaluated each time X increments and other statements within the state will have a chance to run at that time. In the second, the If statement will not be re-evaluated until after the WhileLoop finishes, holding execution within the script block for all ten increments of X. The point is that a loop that runs a very large number of times within a script can cause the rest of the state to essentially freeze until it finishes. Use loops with care.