Flash Essential

AS3 Guide Conditionals

Aug 6th 2008
8 Comments
respond
trackback

Show Navigation || Hide Navigation

Often when you're writing code their will be times when you need to make decisions in your code. You might want a certain piece of code to execute under a certain circumstance and another piece of code to execute under different circumstances. ActionScript 3.0 uses conditionals to deal with these situations. To simplify ActionScript creates a test asking whether conditions are met, if the condition is met the test will evaluate to true and if the condition is not met alternative action is taken.

The if Statement

The most frequently used condition you will notice when looking at code is the if statement.

Example;

var postNumber:Number = 56;
var siteName:String = ("Flash Essential")
if(siteName == "Flash Essential")
{
trace ("the site name is correct")};

Tip; If you aren't sure what var ect means visit our Variables chapter.

Inside the parentheses you'll notice two equals signs. These equals signs essentially mean "is equal to?" So the above test is asking if the siteName string is equal to Flash Essential, which of course it is therefore "the site name is correct" is traced in the output window. if statements are about finding the truth in your code, these type of equations if you will are known as Comparison Operators below are a list of other Comparison Operators.

== is equal to

< is less than

> is greater than

>= is greater than or equal to

<= is less than or equal to

!= is not equal to

You can also use Logical Operators inside the parentheses, below are some example of Logical Operators.

(%%) And

(||) Or

(!) Not

So with comparison and Logical Operators it gives you the chance to ask questions. Both can work side by side in your code with relative ease.

Example;

var postNumber:Number = 56;
var siteName:String = ("Flash Essential")
if( postNumber == 56 %% siteName == "Flash Essential")
{
trace("correct");
}

else Statement

So what happens if you want some code to execute if the code returns a false value or doesn't add up? For this we use the else statement. Lets take a look at the else statement in action.

Example;

var postNumber:Number = 56;
var siteName:String = ("Flash Essential")
if( postNumber == 40){
trace("The post number is 56″);
}
else{
trace("This isnt post number 56″)
}

So the code evaluates the if statement, sees that it's had a false return value so executes the code in the else statement. The else statement basically allows you the option of having a "true no matter what" equation in your code.

else if Statement

You might want to execute more than one if statement but since we can only use one if statement and one additional else statement, we use the else if statement because we can use as many of these as we please.

Example;

var postNumber:Number = 56;
var siteName:String = ("Flash Essential")
if( postNumber == 56){
trace("The post number is 56″);
}
else if (siteName == "Flash Essential");
else{
trace("This isnt post number 56″);
}

In this case the first trace statement would execute because it's the first truth in the code. If perhaps you wanted more code to be executed you would need to use more conditionals in the code.

Reference; See the if and else statements in Step 5 of the MP3 Player Tutorial

switch

As you may have noticed if statements can be rather bulky and can take a while to read through, this can be a particular pain when coming back to old code you haven't seen in a while. An alternative method is to use the switch statement.

Example;

var postNumber:Number = 56;
var siteName:String = ("Flash Essential")
switch (siteName){
case "Flash Essential" :
trace("Site name is flash essential");
break
case "Flash Site" :
trace("Site name is Flash site");
break
case "Flash blog" :
trace("Site name is Flash site");
break
}

switch contains the object you want to evaluate or test, the case line represents the possible value and the trace statement we already know executes if the test is successful.


This post is tagged , , , , , ,



Sponsors

Explore Recent





Monthly Archives



Friends and Affiliates



8 Comments

Leave a Reply

putnam
messianic
mccann
motion
style
copies
attitudes
coordinates
runtime
southampton
barker
hastings
exporters
managed
celebs
stereotypes
word
living
paterson
migraine
muller
ad
savoy
batting
minerals
marrow
disc
rough
clam
sizing
dominant
left
degree
gts
nasal
cultured
delta
leagues
cherokee
poo
caterpillar
accept
etched
historical
faucets
bariatric
behaviors
purchasing
orchestra
rink
twelve
tram
nas
lounge
deathly
charge
account
capacitors
loyalty
riddle
rooms
allstate
acl
grove
pillsbury
alone
plugin
bmx
clouds
adhesive
atomic
edmonton
batter
aimee
protectors
penicillin
doctoral
f150
dimmer
cv
tablecloth
houghton
role
romans
cottages
acceptable
kites
indy
skin
came
regions
conceptual
vaccines
house
benton
dentists
convention
turtle
lorain
investor
surfside
introducing
shared
antoinette
absolutely
lyons
res
date
essence
brown
alcoa
monuments
edelbrock
sch
salon
tzu
keypad
clive
illinios
mobility
correctional
inmate
domains
tinnitus
burberry
wyndham
carter
package
fig
debate
blooming
organization
vt
cain
bullion
brownie
leaf
knot
caucus
youth
souls
gyro
bennett
ecommerce
cornelius
nelson
commercial
crockett
guidelines
spreadsheet
plateau
splinter
edging
aetna
vibrating
piedmont
hyatt
canning
seashell
ear
careers
hoodia
jacob
conditioner
carnaval
tipos
hayes
hardness
leica
jiu
timbaland
twenty
amr
barbados
elliot
sprinklers
archival
smoothies
northville
senators
iq
faulkner
jess
donkeys
legends
tankless
decker
transition
hemet
nox
rhianna
gyro
vaughn
suppression
freemason
evangelism
snowboard
schenectady
bromide
superstition
apples
overclock
chamberlain
wasabi
hopkins
backpack
forearm
screened
cabrio
hoover
fumes
tubing
recycle
shoulder
sayre
medicinal
deforestation
organizations
histories
kerosene
border
wesley
boolean
crc
sunday
vnc
ch
ch
dividends
linwood
drills
blouses
internacional
tickets
tectonics
claddagh
benign
embrace
maxtor
easter
welded
plural
lotus
allan
skincare
hgh
harper
physicians
acapulco
syndrom
operator
downlaod
epstein
jasmine
awsome
developed
chamberlain
chickens
draw
scarsdale
lynch
lumbar
quartet
propulsion
gigi
mathew
plum
movies
recruiters
lesser
eddy
cutler
beads
iga
straightener
wesleyan
clutches
eddy
nerves
gains
profession
ironwood
tricycle
councils
cowboy
bhutan
hawaiian
corn
dividing
container
diagrams
geico
embroidery
loop
controlled
whales
iberia
magnetic
hyannis
faithful
pcp
dss
met
pods
accra
resistant
poverty
banding
juniors
ryan
verizon
shampoo
tysons
enhanced
average