Conditional Statements

[ LiB ]

What are conditional statements? You already encountered them when you went over pseudo code in the last chapter. Remember that? Remember the if statements that you used to control the program's logic? Those are considered conditional statements. They govern the flow of the program depending on certain variable conditions. These are very powerful structures; pay close attention to them here.

The if statement is a conditional statement that does a comparison. If this comparison results in a true Boolean value, then the if code block is executed. If the result is false, the whole block is ignored.

That's great, but what do I mean by a code block ? A block of code is any code segment that is surrounded by curly brackets. Let's look at a simple if statement.

 if (a > 5) {   trace("a is greater than 5");   trace("Now Exiting"); } 

So what's going on here? The first line reads, "If a is greater than 5, execute the following block." Note that the parentheses must go around the comparisons. Assuming that "a" was initialized and manipulated prior to this conditional statement, let's say "a" had a value of 6. Just to restate : "If 6 is greater than 5, then output 'a is greater than 5' on one line and 'Now Exiting ' on another line." Notice how I always replace the variable with its value when I'm double-checking. This way, I can clarify what the computer is seeing.

There is more to this if statement than you think. Most of the most complex logic you will ever find is built around if statements. The if statement also has an else clause. The else clause catches everything the first block didn't catch. Look at the following example so you can better understand what I am talking about:

 if (a > 5) {   trace("a is greater than 5"); } else {   trace("a is less than or equal to 5"); } 

Allow me to restate the logic. The program tells the computer to output the result. If a is greater than 5, then it outputs "a is greater than 5." In any other case, it will output "a is less than or equal to 5."

You can also embed the if statements and also combine them and use else if clauses for precise logic. Let's jump into another example.

NOTE

NOTE

Notice that the brackets are placed around the single trace statements in each clause. This syntax conven tion is not needed when you have a block consisting of one statement it is just good practice.You'll be sur prised how many bugs you'll bump into just because of a missing curly bracket .

 if (a == 5)   trace ("a is now 5"); else if (a == 6)   trace ("a is now 6"); else if (a == 7)   trace ("a is now 7"); else   trace("a is unknown"); 

Notice that I did not use curly brackets to denote each of the blocks in the last conditional structure. I left them out for the purpose of demonstration, but I suggest you always use curly brackets after your if statements if you are not sure if you should use them or not you will soon get the hang of creating your own code blocks. Soon enough, I will show you a better way to construct a long conditional structure with the switch statement.

There are four tests in this structure. If any of them are true, they end up doing the same thingoutputting what a is equal to. The first one tests to see if a is equal to 5. If it is, Flash outputs 5. If a is not equal to 5, the code then tests to see if a is equal to 6. If a is 6, Flash lets you know by outputting; if not, Flash goes on to test if a is equal to 7. If a is not equal to 7, the program flows to the final else statement. This statement is executed if the answer was none of the above. Follow me so far?

Aside from mixing and matching, you're also allowed to combine comparisons with logical operators. These are the and (&&) and or () operators. They don't make too much sense in text, so let's see them in action.

 if ((temp01 == 1) && (temp02 == 5)) {   trace("temp01 is equal to 1 and temp02 is equal to 5."); }   if ((temp01==1)  (temp02 == 5)) {   trace("Either temp01 is equal to 1 or temp02 is equal to 5"); } 

If you understand these without my explaining them, you are close to becoming an expert. If not, don't worry.

Notice the wording of the output. I gave you clues to as to when the if statements evaluate to true. The and conditional statement won't execute its block unless both parts of the conditions are true. The or conditional statement will execute its block if either one of the statements is true. You'll create a need for these once you start letting your imagination run wild.

The switch Statement

The switch statement is a good alternative when it comes to long conditional structures. It is not as popular as the if statement, but it will come in handy when you need it. Let's look at an example.

 switch (tempVar) {   case 6:    trace("tempVar is 6.");    break;   case 7:    trace("tempVar is 7.");    break;   case 8:    trace("tempVar is 8.");    break;   default:    trace("tempVar is unknown."); } 

Don't be alarmed. The switch statement is just as simple as the if statement, though it looks more complex. Once you understand its anatomy, you will be comfortable using it. Allow me to break it down for you.

In order to set up the structure, you must type out the keyword switch and within its parentheses you must type in the variable that is going to be tested . A special block follows the keyword and variableit is a case block that uses special keywords that work together with the rest of the statement.

The break statement you see in the code forces the code to jump out of the current structurein this case, the switch statement. If the break statement wasn't used, Flash would keep executing the consecutive commands that follow.

NOTE

TIP

Don't leave out the break statement at the end of each case unless you know what you are doing. If you for get it, it will execute all the cases after the one it matched. Be careful!

The case keyword is followed by the value that we are testing against. In our example, tempVar is tested to see if it is equal to 6 in the first case statement. If it is, it executes everything after the case statement until it reaches the break statement. Once it does, it exits the structure and continues with the rest of the program. If it's not the current case, it goes on to the next case until it finds a match. If it doesn't find a match, it jumps to the default segment. This is the "catch-all" statement that handles all the values that weren't covered.

NOTE

NOTE

Notice that all cases in the switch statement end in a colon .

Now that you have a good solid foundation with conditional statements, you can move on to loop structures. Don't worry about the switch statement too much. It doesn't get any more complicated than what you saw here. Most statements require exposure in order for you to become completely comfortable with them.

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net