Programming with ActionScript 3: Flashosophy Cram Session Part II (Conditions & Operators)
Introduction:
Conditions can be used when you want to make a decision between two or more choices. Let's cut the BS and get right to it.
Section 1: Conditions - The 'if' Statement
'if' statements return a Boolean. That means we can only get a true or false from the condition. If the condition is true, then the code inside the if statement will execute, otherwise, the code inside the curly braces will be ignored. There are several ways to say the same thing in an 'if' statement:
//If the light is green, go.
var green:Boolean = true;
//first way of checking if green is true
if(green)
{ //beginning of curly braces
trace("go 1"); //this will happen because green is true
} //end of curly braces
//second way of checking if green is true
if(green == true) //notice that 2 equal signs are used to compare booleans
{
trace("go 2"); //this will happen because green is true
}
/*
putting an exclamation mark in front of a boolean inverts the condition. An exclamation means 'not'. This statement says: "if not green". Or rather, "if not true". So the results of this statement is false. Because it is false, "go 3" will not be displayed in the output.
*/
if(!green){ //some people like to put the curly braces up here to save a line of space
trace("go 3"); //this will not happen because the condition is not true
}
//you can also write it this way
if(green == false){
trace("go 4");
}
//you can also put a 'not' when comparing booleans. != means not equals.
if(green != false){ // statement: true not (false) = true
trace("go 5"); // because the statement is true, this will show.
}
Compile the above code by pasting it into the actions panel in frame 1 and do a ctrl-enter. You should get:
go 1
go 2
go 5
Section 2: Conditions -The 'if else' Statement
'if else' statements work like this: If this is true, then execute the code in my curly braces, else do this other section of code. This is how it works:
var hungry:Boolean = false; //not hungry
if(hungry){ // are you hungry?
trace("Eat stuff");
}
else //if not hungry, then do this
{
trace("Learn Flash");
}
//you can have nested if else statements
var starving:Boolean = false;
var thirsty:Boolean = true;
if(hungry){ // are you hungry?
trace("Eat stuff");
}
else // if not hungry,
if(starving){ //then are you starving?
trace("Eat what you see");
}
else //if not starving,
if(thirsty)
{ //then are you thirsty?
trace("Drink Water"); //yes it is true! Drinking water~!
}
else //otherwise, do this
{
trace("Learn Flash");
}
Compile the above code by pasting it into the actions panel in frame 1 and do a ctrl-enter. You should get:
Learn Flash
Drink Water
I can still check for the same thing with basic 'if' statements, so who needs 'if else' statements? Yes it is true that you can achieve the same thing with just if statements to check for things that are true or false. However, 'if else' statements are more efficient because the entire nest of statement ends as soon as a statement is true. You would use an 'if else' statement if you want to see if one thing is true in a list of choices provided. You can have as many nested 'if else' statements as you'd like.
Section 3: Conditions - The 'switch' Statement
Having large nested 'if else' statements take a toll on CPU resources. In other words, it is slow. A 'switch' statement solves this issue. Switch statements was a bit weird for me to use when I was learning how to code. But now, I use them whenever I can in replacement of 'if else' statements. Best way to learn is skip the BS and show an example:
var numWarnings:int = 2;
switch(numWarnings){
case 0: //the following code executes if numWarnings is 0
trace("No warnings");
break; //end of case 0
case 1://the following code executes if numWarnings is 1
trace("First warning");
break;//end of case 1
case 2:
trace("Second warning"); //this is the output because numWarnings = 2
break;
case 3:
trace("Third warning");
break;
default: //if numWarnings is not 0, 1, 2, or 3, then execute the following
trace("KaBOOM");
break;
}
The parameter (the stuff you put inside the braces of the switch statement) of the switch statement can be use with any variable: int, uint, Number, Boolean (but that is dumb because you can just use if statement instead), and strings. You can put more than just variables in the switch statement but I've never used it like that so I won't go over it.
Section 4: Operators
In section one, you learned about == and !=. These are called operators. Operators are used to achieve a boolean result. Here is the full list of operators.
== (equals)
!= (not equals)
> (greater than. This is shift-comma on the keyboard)
< (less than. This is shift-period on the keyboard)
>= (greater than or equal to)
<= (less than or equal to)
|| (or. This symbol is on the shift ' \' on the keyboard.)
&& (and)
Example:
trace( 2==1) // this outputs false obviously
trace( 2 != 3) //true
trace( 2 > 3) //false
trace( 2 < 3) //true
trace( 2 >= 3) //false
trace( 2 <= 2) //true because 2 is not less than 2, but it IS equal 2
When using the || operator, keep in mind this trick:
1) you get TRUE when one of the statements are true
2) you get FALSE only when all the statements around the || is false
trace( (1==1) || (2 > 1) ) //true because first statement is true
trace( (1==2) || (2 > 2) || (1 > 1) ) //false because all are false
When using the && operator, keep in mind this trick:
1) you get FALSE when one of the statements are false
2) you get TRUE only when all the statements around the && is true
trace( (1==1) && (2 > 2) ) //false because the second statement is false
trace( (1==2) && (2 > 1) && (1 > 1) ) //false because not all are true
trace( (1==1) && (2 > 1) && (1 >= 1) ) //true because all are true
Do you think you got it? Can you follow why this is true?
trace( (1==1) || (2 < 1) && (3 == 2) || (5 < 2) && (2 == 2) )
Well, if you did follow through, I don't know how you did it because I sure don't understand that. Adding parentheses can yield different results depending on where you put them. It is recommended that you use parentheses (don't use brackets) so you can follow your logic.
trace( ((1==1) || (2 < 1)) && ((3 == 2) || (5 < 2)) && (2 == 2) ) //this returns false.
//the above has the same logic as below but simplified
trace( (true || false) && ((false) || (false)) && (true) ) //we can chunk it some more
trace( true && false && true ) //because it is not all true, we get false
Ok now lets put the parenthesis another way:
trace( (1==1) || ((2 < 1) && (3 == 2)) || ((5 < 2) && (2 == 2))) //this returns true.
//the above has the same logic as below but simplified
trace( (true) || ((false) && (false)) || ((false) && (true)))//we can chunk it some more
trace( (true) || (false) || (false)) //because one of the set of || is true, we get true
If we don't specify the order of operation with parentheses, the && operation comes before the || operation. Think of && is multiply and || as addition. Multiplication and division always gets operated before addition and subtraction.
Did I lose you? Are you lost? If you are, it is ok. Some people are not good with numbers. The next section should clear things up a bit more.
Section 5: Conditions with Operators
Who would use all those || and && with numbers anyway right? Well, almost every developer will have to use it. But for now, lets use English to help understand all this.
var neckPain:Boolean = true;
var backPain:Boolean = true;
var painLevel:uint = 3;
var hungry = false;
var thirsty = false;
//if hungry and thirsty and not neckPain or backpain, go get some food
if( hungry && thirsty && !(neckPain || backPain) ){
trace("Go get some food");
}
else
//otherwise, if neckPain and backpain and painLevel is equal to or more than 3, go see a doctor
if( neckPain && backPain && painLevel >= 3){
trace("Go see a doctor!");
}
else //otherwise, learn Flash
{
trace("Learn Flash");
}
By now, you should be able to trace the above code by hand and know the results of the output. If not, you need more time to absorb this cram session.
Ending Summary
This cram session is one of the most important of the basics. Understand this tutorial well before you move on to the next level.
You are now +=2 steps closer to being a pro-programmer.
Published On: July 15, 2008
Next Chapter: Flashosophy Cram Session Part III (Arrays and Loops)
Previous Chapter: Flashosophy Cram Session Part I (Variables and Math)
(c) 2008 Flashosophy