Chapter 11. Making Decisions in Code

Write an If Statement

In 1979, an extremely popular series of books targeted at young adults hit the shelves. The first such book, written by Edward Packard, was called The Cave of Time, and it introduced a novel new concept in which, at the end of each page, readers had to choose between two alternatives. Based on their choice, they jumped to a different page of the book, so no two readings of the book were the same. Eventually, there would be well over 100 of these Choose Your Own Adventure titles published.

ActionScript, like other programming languages, provides the ability to have the script make decisions based on what is currently happening in the movie, much like readers of those books could choose their path through the story. The most common decision-making statement is if. The if clause enables you to provide a test, and if that test is true, the statements enclosed within the if's curly braces will be executed; if not, processing will continue below the statement.

The test can be almost anything. You can see if the current value of a variable is greater than or less than another variable, or a hard-coded value. You can see if the text property of a text field is empty, or if a variable has been defined, or if an instance of an object has been created.

When comparing two values for equality, you need to be careful to use the equality operator, which is two equals signs (==). A single equals sign sets the value of the variable to its left to the value to its right; two equal signs compare the value to the left with the value to the right.

Write an If Statement

WRITE A STATEMENT THAT COMPARES TWO VALUES FOR EQUALITY

  • WRITE A STATEMENT THAT COMPARES TWO VALUES FOR EQUALITY
  • WRITE A STATEMENT THAT COMPARES TWO VALUES FOR EQUALITY
  • WRITE A STATEMENT THAT COMPARES TWO VALUES FOR EQUALITY
  • WRITE A STATEMENT THAT COMPARES TWO VALUES FOR EQUALITY
  • WRITE A STATEMENT THAT COMPARES TWO VALUES FOR EQUALITY
  • WRITE A STATEMENT THAT COMPARES TWO VALUES FOR EQUALITY
  • WRITE A STATEMENT THAT COMPARES TWO VALUES FOR EQUALITY
  • WRITE A STATEMENT THAT COMPARES TWO VALUES FOR EQUALITY
  • WRITE A STATEMENT THAT COMPARES TWO VALUES FOR EQUALITY
WRITE A STATEMENT THAT COMPARES TWO VALUES FOR EQUALITY

TEST THE STATEMENT

  • TEST THE STATEMENT

    The movie plays.

  • TEST THE STATEMENT
  • TEST THE STATEMENT
  • TEST THE STATEMENT
  • TEST THE STATEMENT

    The movie plays.

  • TEST THE STATEMENT
TEST THE STATEMENT

Apply It

In addition to the equality operator (==), you can use other operators to compare values, as shown in the following:

if(num1 > num2) //tests to see if num1 is greater than num2.
if(num1 < num2) // tests to see if num1 is less than num2.
if(num1 >= num2) // tests to see if num1 is greater than or equal to num2.
if(num1 <= num2) //tests to see if num1 is less than or equal to num2.
if(num1 != num2) // tests to see that num1 does not equal num2.
if(num1 is String) // tests to see if num1 is of data type String.
if(num1 === num2) // compares values without converting data types. If num1 is 3 (a String) and num2 is 3 (a Number), then num1 == num2 is false while num1 === num2 is true.

Using Else and Else If Clauses

A simple if statement contains code that will execute if the condition is true; otherwise, processing will continue below the statement. However, ActionScript also includes the ability to provide other options. You can use an else clause to say, "if the test is true, do this; otherwise, do something else."

The else clause follows the closing curly brace of the if statement and contains its own set of braces. The code to be executed by the else clause — the code to execute if the if condition evaluates to false — is contained within those braces.

For example, say that you have a login script. You will want to have two scenarios: one if the users provide the correct credentials and another if they do not. You could therefore write the if statement, testing their inputversus the known accepted credentials, and take whatever steps are appropriate if they provide the correct information. However, you do not want to do nothing if they do not, so in an else clause, you might provide an informational message stating that their username and password did not match those on file.

You can also test on more than one possible condition using one or more else if statements. Like if, the else if clause contains a test and, within braces, a set of statements to execute if the test is true. But like else, the else if clause will execute only if the if statement — and any else if statements above it — have evaluated to false.

You can have as many else if clauses as you need, but they must appear after the if clause, and before the else clause if it is present.

Using Else and Else If Clauses

  • Using Else and Else If Clauses
  • Using Else and Else If Clauses
  • Using Else and Else If Clauses
  • Using Else and Else If Clauses
    Using Else and Else If Clauses
  • Using Else and Else If Clauses
  • Using Else and Else If Clauses
  • Using Else and Else If Clauses
  • Using Else and Else If Clauses
  • Using Else and Else If Clauses
  • Using Else and Else If Clauses
  • Using Else and Else If Clauses
    Using Else and Else If Clauses
  • Using Else and Else If Clauses

    The movie plays.

  • Using Else and Else If Clauses

Extra

Any expression that evaluates to true or false can be used in an if clause. Boolean values are of course already true or false, so they can be evaluated as a simple value. For example, if you had var adminStatus:Boolean = true, then if(adminStatus) would evaluate to true. You can test on the opposite result using the ! character, so if(!adminStatus) would be false, as you are in essence saying "if the value of adminStatus is not true."

Other data types can also be evaluated directly in the same manner, but you need to be careful that you understand the implications. For example, if you have a variable num1 set as a Number, then if(num1) will be true so long as the value of num1 is not zero or null; otherwise, it will be false. Strings will evaluate to true as long as they are not empty or null. The same applies to arrays: They are true as long as they are not null and not empty. Most complex data types will evaluate to true as long as they are not null, so if(mcSquare1) will be true if a MovieClip exists with that instance name and unless you had explicitly written mcSquare1 = null.

Test Multiple Conditions

Within a single test clause, you can test on multiple conditions using a set of special Boolean operators. These can be helpful when you need to perform one set of actions if two or more conditions are true or if one out of a choice of conditions is true.

The logical AND operator, &&, is used to see if two or more conditions are all true. For example, if you wanted to test to see if a variable shipName contained "Serenity" and a variable captainName contained "Mal", you could write

if(shipName == "Serenity" && captainName == "Mal")

Note that in this case, both conditions must be true. If either is false, then the entire expression is false. If you have a situation in which you need only one of the two values to be true, you could use the logical OR operator, ||:

if(shipName == "Millennium Falcon" || captainName == "Lando")

In this case, one of the conditions can be false, and the expression will still evaluate to true, as long as the other is true. The expression will evaluate to false only if both conditions are false.

The logical OR operator is two pipe characters. On most keyboards, the pipe character is located directly above the Enter key, on the same key as the backslash. Keyboards tend to label the key as a broken vertical line, but when typed, it will be displayed as an unbroken line.

Test Multiple Conditions

  • Test Multiple Conditions
    Test Multiple Conditions
  • Test Multiple Conditions
  • Test Multiple Conditions
  • Test Multiple Conditions
  • Test Multiple Conditions

    Note

    See Chapter 9 for details on the indexOf method.

  • Test Multiple Conditions
  • Test Multiple Conditions
  • Test Multiple Conditions

    The movie plays.

Test Multiple Conditions
  • Test Multiple Conditions

Apply It

You can test on more than two conditions by combining expressions using the && and || operators. For example, this is legal:

if(num1 == num2 || num2 >  num3 && num3 < num4)

However, you should note that the preceding statement will not execute from left to right. When you combine both logical AND and logical OR statements, the AND statements are evaluated first. Therefore, the preceding condition does not read, "If num1 equals num2 or if num2 is greater than num3 and if num3 is less than num4." Rather, it actually reads, "If num2 is greater than num3 and num3 is less than num4, or if num1 is equal to num2." Often, this distinction will not matter, but it is important to carefully evaluate your expressions in case it does.

If you need to force the logical OR expressions to be read first, you can group them using parentheses:

if((num1 == num2 || num2 > num3) && num3 < num4)

Replace If/Else Clauses with a Switch Statement

If you have a long series of conditions that need to be tested, you might be able to save some code by replacing your if/else clauses with a switch statement. The switch statement does the exact same thing as a series of if/else clauses: It evaluates each and, if true, executes the code indicated. However, the switch statement in general uses less code.

To write a switch, you begin with the switch keyword, followed by the variable that you want to test in parentheses:

switch(userName)

Then, within a set of curly braces, you will include a series of case statements. Each case contains the value against which the variable in the switch should be compared. If they are equal, the statements following the case are executed.

Even if the switch statement finds a case that is equal to the value being compared, it will continue to look at the remainder of the cases. Therefore, you need to include a break clause with each case, which stops the execution of the switch.

The switch statement can end with a default case, which is the equivalent of an else clause in your if statement: It is the code to be executed if none of the other cases matched.

Two significant limitations of switch are that it can test only for equality and only against a single variable: The cases are either equal to or not equal to the variable. If you need to test for inequality or against multiple conditions, you need to use an if statement with if/else clauses.

Replace If/Else Clauses with a Switch Statement

  • Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement
    Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement

    The movie plays.

Replace If/Else Clauses with a Switch Statement
  • Replace If/Else Clauses with a Switch Statement

Apply It

You can test against more than one case at a time by stacking case statements. In order to display one message if a user is in either the Pacific or Mountain time zones, for example, you might have the following:

case "PST":
case "MST":
                   trace("You are in the Pacific or Mountain time zones");
                   break;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.191.169