Week 9: In Class Practical – Conditional Statements

For each exercise, create a new file and name as directed. When you are done place all files in a folder with your name inside the week 9 practical folder on dropbox.

In this practical we are going to learn to make decisions in our code. The way we make decisions in programming is through conditional statements. Basically in plain english the code works like this:

If this is true, then do this.

In code it looks like this:

if(<YOUR CONDITION HERE>) {
//Do stuff
}

Exercise 1

• Create a new flash file and name it conditions.fla
• set up your actions layer and open the actions panel
• Create a variable called myBool with the data type, Boolean and set it equal to true
• now write a conditional statement testing myBool
• Inside the condition block (aka the curly braces) run a trace statement outputing a string saying “myBool is equal to true”
• Test the movie and you should see the message appear in the output window
• Now try setting myBool equal to false and test the movie again. you should not see the message appear now

Exercise 2

So testing if a variable equals true is pretty basic, to step it up a notch lets use some conditional operators:

> Is Greater than
< Is Less than
== Is Equal to
• Create a new flash file and name it conditional-operators.fla
• set up your actions layer and open the actions panel
• Create twos variable called num1 and num2 with the data type Number, and set num1 equal 1 and num2  equal to 2
• now write 3 conditional statements each using a different conditional operator (>, <, ==) testing the two variables, num1 and num2
• Inside each of the 3 condition blocks (aka the curly braces) run a trace statement outputing a string that explains the condition.
• The Is Greater Than condition message should be “num1 is greater than num2″
• The Is Less Than condition message should be “num1 is less than num2″
• The Is Equal To condition message should be “num1 is equal to num2″
• Test the movie and you should  only see the message  form the Is Less than condition appear in the output window. That is because 1 is less than 2.
• Now try changing the values of num1 and num2 to and test the movie again and see what messages appear

Exercise 3

Ok now what if we want to do something no matter what? So in plain english:

If this is true, then do this
if it isn't true, do this instead

In code this is called an if-else conditional statement, which allows us to run one set of code if the condition we test is true and another set of code if the condition is false. it looks like this:

if(<YOUR CONDITION HERE>) {
//Do stuff
} else {
//Okay, do this instead
}
• Create a new flash file and name it if-else-conditions.fla
• set up your actions layer and open the actions panel
• Create twos variable called num1 and num2 with the data type Number, and set num1 equal 1 and num2  equal to 2
• now write an if-else conditional statement testing num1 IsGreater Than num2
• Inside the if condition block (aka the curly braces) trace the message “num1 is greater than num2″
• Inside the else condition block (aka the curly braces) trace the message “num1 is not greater than num2″
• Test the movie and see what message appears
• Now try changing the values of num1 and num2 to and test the movie again and see what messages appear now