Tài liệu Controlling a Script''''s Flow - Pdf 87


< 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

} 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

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");

}


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status