In the previous section, we learned about the basic variable types, and how to use them.
In this section, we’ll look at conditional statements, used to make decisions in programs.
Conditionals (If and Else)
So we now know what variables are, the next interesting thing is being able to chose to do with them.
Lets take an instance of someone getting a score in a game, and chosing if it’s high enough to go on the list of high scores.
var lowest_high_score:int=100;
var billys_score:int=12;
if(billys_score>lowest_high_score)
trace("Billy got a high score!");
else
trace("Billy did not make the high scores list.");
if(billys_score==0)
{
trace("Billy did not make the high scores list.");
trace("and Billy got no points!");
}
//this will trace out:
//'Billy did not make the high scores list.'
//Because wis score was below the high score, and wasn't 0.
There are a few new things here. First off, checking the value on something. You have to compare two different values to decide what to do.
We start off with using the ‘if’ statement to see if something it true. If it is, it will run whatever is right below it (only the first line in this case).
When we use open and closed squiggly brackets (‘{‘ and ‘}’), we will do whatever is in between them if the if statement is fround true.
Here, we create two variables, one for the lowest high score to get a high score, and another to store ‘billys’ score.
Next, we check to see if ‘billys_score’ is greater than the lowest score. If so, we trace out “Billy got a high score!”.
if not, we’ll instead trace out ‘Billy did not make the high scores list.’.
After that, we’ll check if billys score is 0, which it isn’t so we won’t run anything inside the brackets.
‘if’ statements are used to check if something it true, and if so, run whatever line is below them, or whatever is in squiggly brackets.
‘else’ statements are always used with ‘if’ statements. Anything immediately below them, or in squiggly brackets will run if the ‘if’ statement above it was found false.
Comparison Operators
When you want to compare objects, there are several different opertors you can use:
‘>’ :Used to check if the value of the stuff on the left side is greater than the value on the right of the operator (exp: (4>3) is true, (3>4) is false).
‘<' : The opposite of the previous operator. Checks if the right side is greater than the left side.
‘>=’ : Checks is the left side is greater tha the right side, or if they are equal.
‘<=' : The opposite of the previous operator.
‘==’ : Checks if both sides are the same (such as (0==0) is true, (89==89) is true, (89==0) is false.
Conditionals
The conditionals that can be used are:
if Used to check if something it true.
else Used after an ‘if’ conditional if the ‘if’ returned false.
else if Used after an ‘if’ conditional if the ‘if’ returned false, but will only run if something else if fonud true.
An Example:
var lowest_high_score:int=100;
var billys_score:int=12;
var timmys_score:int=223
var homers_score=100;
if(billys_score>lowest_high_score)
trace("Billy got a high score!");
else if(billys_score==lowest_high_score)
trace("Billy barely made the high score list...");
else
trace("Billy did not make the high scores list.");
if(timmys_score>lowest_high_score)
trace("Timmy got a high score!");
else if(timmys_score==lowest_high_score)
trace("Timmy barely made the high score list...");
else
trace("Timmy did not make the high scores list.");
if(homers_score>lowest_high_score)
trace("Homer got a high score!");
else if(homers_score==lowest_high_score)
trace("Homer barely made the high score list...");
else
trace("Homer did not make the high scores list.");
//this will print out:
//Billy did not make the high scores list.
//Timmy got a high score!
//Homer almost made the high score list...
Here we first check if the persons high score is above the lowest high score, if it is not, than we check if it is equal to the lowest high score, and if that isn’t true, we assume that they did not make the high score list, and was lower.
Checking Multiple things using boolean logic (AND, OR, NOT)
The if statement comes in handy if you want to check anything. But what happens if you want to check multiple things?
For instance what if you want to find out if someone is older than 30, but also younger than 40? You could either use two nested(one inside the brackets of the other) ‘if’ statements, but a better way is using boolean logic.
There are a few types you need to know:
AND (&&) : ‘And’ is used by creating two ampersand symbols. For example: if(age>30&&age<40)
OR (||) : ‘Or’ is used by creating to ‘pipe’ symbols (shift+backslash – key above enter on my keyboard).
You use it in the same way you use and, but it will return true if either statement is true.
For an example: if(grade>70||grade==65)
NOT (!) : ‘Not’ is used with an exclamation point. It is used:
‘if(score!=100)’
An example of all three:
var mans_height_feet:Number=5.5;
if(mans_height_feet<5&&mans_height_feet>4)
trace("you are a midget");
else if(mans_height_feet==5.1||mans_height_feet==5.2)
trace("you are very short");
else if(mans_height_feet<5.5&&mans_height_feet!=5.4)
{
trace("you must be 5.3 feet tall!");
trace("you're still short");
}
else if(mans_height_feet>6.5&&mans_height_feet<7.5)
{
if(mans_height_feet>7)
trace("you seem to be a partial giant");
else
trace("you seem to be a full giant");
}
You will commonly use “if’s” to check something, whether it be whether the game is over, if they entered valid text, ect.
Most of the time you’ll either use an ‘else’, or an ‘else if’ after your if statement, but it is not required.
Switch Statements
If you need to do many “if’s” and “else if’s”, you can use a ‘switch’ statement instead to save some room coding.
For instance:
var age:int=35;
//checking age using if's, and if else's
if(age==34)
trace("you are an old maid");
else if(age==33)
trace("wow, you're young!");
else if(age==25)
trace("you're not too old yet!");
//checking age using a switch statement
switch (age)
{
case 34:
trace("you are an old maid");
break;
case 33:
trace("wow, you're young!");
break;
case 25:
trace("you're not too old yet!");
break;
}
The switch statement ocasionally comes in useful when you need to check a lot of different possibilities, but you’ll find you usually don’t have to use it too often.
Again a little mistake =P
’>=’ : The opposite of the previous operator.
should be
’<=’ : The opposite of the previous operator.
thanks for this article too !! =D
Awesome, thanks for seeing that!
Was planning on having a couple people going through the articles start to finish when they’re done, but good to get those mistakes corrected!
I really appreciate the effort put into these tutorials. I was specifically looking for something that covers programming concepts through the lens of AS3.
I humbly suggest following through on that proof reading. This page (part 4) in particular is a typo explosion.
Hi first of thanks for the promising tutorials! So far I’ve enjoyed them. But there seems to be a problem with viewing your symbols in the code text.
Here’s an example:
if(mans_height_feet<5&&mans_height_feet>4)
I tested it in both Chrome and IE but it showed up the same in both browsers..
I have figured out what “<”, “&” and “>” means but do you have any idea why it shows up like that? It makes it harder to read and I’m making a lot of typo’s as it is. 😛
Ah, thanks for seeing that-
WordPress has a WYSIWYG (what you see is what you get) editor, that seems to go through and replace the improper symbols in my code blocks to proper html symbols- but the code block does it also, so it causes those problems 😮
I turned off the WYSIWYG editor, so I thought it stopped doing that, but I guess not.
Let me know if you see any other tutorials that change like that-
But if you do, and don’t want it to interfere until I have a chance to fix it, copy and paste the article into notepad++/ wordpad (or any text editing program aside from notepad) do find and replace (usually ctrl+h):
> to >
< to < & ; to & and it should fix them, at least so it's usable. I'll check the others and see if they've changed too- thanks!