< Day Day Up >
Controlling a Script's Flow
Typically, actions in your scripts execute consecutively, from beginning to end—a
sequence that represents your script's flow. Using conditional logic, you can control this
flow by scripting specific actions to execute only when specific conditions are met or
exist in your movie. By implementing conditional logic in your scripts, you give your
movie the ability to make decisions and take action based on various conditions you've
set, and your movie takes on more dimension as a result. You'll use the conditional
statements or phrases described in this lesson to implement conditional logic in your
scripts.
If/Then Statements
At the heart of conditional logic is the simple if/then statement. Here's an example:
if (moneySaved > 500) {
buyStuff();
}
// next line of actions...
The buyStuff() function is called only if the variable moneySaved has a value greater
than 500. If moneySaved is equal to or less than 500, the buyStuff() function call is
ignored and actions immediately below the if statement are executed.
At its core, a conditional statement looks at a circumstance (placed within parentheses)
and determines whether that circumstance is true or false. If the circumstance is true,
actions within the statement are executed; if the circumstance is false, the actions are
ignored. When you create an if statement, you state, essentially:
if (...what is shown here is true) {
Do this;
}
The data you place within parentheses represents the condition to be analyzed. The data
within the curly braces ({}) represents the actions to be taken if the condition exists.
As in real life, sometimes an if statement needs to analyze multiple conditions before
taking a single action or set of actions. For example:
if (moneySaved > 500 && billsPaid == true) {
buyStuff();
}
The AND operator (&&) has been added to the statement so that now the buyStuff()
function is called only if moneySaved is more than 500 and billsPaid has a value of true.
If either condition is false, buyStuff() will not be called.
Using the OR operator (||) allows you to take a slightly different approach:
if (moneySaved > 500 || wonLottery == true) {
buyStuff();
}
The buyStuff() function is called if either moneySaved has a value greater than 500 or
wonLottery has a value of true. Both conditions need not be true for the buyStuff()
function to be called, as was the case when using the AND operator (&&) in the earlier
example.
You can mix the AND and OR operators to create sophisticated conditional statements
like this one:
if (moneySaved > 500 && billsPaid == true || wonLottery == true) {
buyStuff();
}
In this script, the buyStuff() function is called only if moneySaved is more than 500 and
billsPaid has a value of true, or if wonLottery has a value of true.
The following table shows a list of the common operators (known as comparison
operators because they're used to compare values) used in conditional logic, with brief
descriptions and examples of how they're used.
OPERATOR DESCRIPTION EXAMPLE EXECUTE THE
FUNCTION IF…
== Checks for
equality
if (name == "Derek") name has an exact value of
Derek
!= Checks for
inequality
if (name != "Derek") name has a value other than
Derek
< Less than if (age < 30) age has a value less than 30
> Greater than if (age > 30) age has a value greater than
30
<= Less than or equal
to
if (age <= 30) age has a value less than or
equal to 30
>= Greater than or
equal to
if (age >= 30) age has a value greater than
or equal to 30
&& Logical AND if (day == "Friday"
&& pmTime > 5)
day has a value of Friday
and pmTime has a value
greater than 5
|| Logical OR if (day ==
"Saturday" || day ==
"Sunday")
day has a value of Saturday
or Sunday
A common mistake when checking equality is to insert a single equals sign (=) where a
double equals sign (==) belongs. Use a single equals sign to assign a value (for example,
money = 300). Use a double equals sign to check for equality: money == 300 does not
assign a value of 300 to money. Rather, it asks whether money has a value of 300.
NOTE
Although number comparisons are straightforward—after all, most of us understand that
50 is less than 100—text-value comparisons are less obvious. Derek doesn't equal derek
even though the same letters are used. With string values, A has a lower value than Z, and
lowercase letters have greater values than uppercase letters. Thus, if A has a value of 1, z
has a value of 52
(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz).
If/Else If Statements
An if/else if statement is similar to the basic if statement except that it enables your script
to react to multiple conditions. Here's an example:
if (money > 500) {
buyTV("35 inch");
} else if (money > 300) {
buyTV("27 inch");
}