Coder Profile - Show off your skills, get a coder profile.
 
 
 
if/else, switch/case and ?/: Statements In Actionscript 2.0
Programming
In Macromedia Flash, using if/else statements (and the substitutes) are quite similar to other programming languages. Using an if statement is easy:


if (statement) {
//actions
}

If the actions between the brackets is true [variable equals 100], then do the following actions.

You can also use if/else statements; in two ways:


as an else if statement:

if (statement) {
//actions
} else if (statement) {
//actions
}

If the actions between the brackets is true [variable equals 100], then do the following actions, or if the actions between the second brackets is true [variable equals 200], then do the following actions.


..Or as an if/else statement:

if (statement) {
//actions
} else {
//actions
}

If the actions between the brackets is true [variable equals 100], then do the following actions, otherwise do the following actions.


Pretty simple, eh?


Switch/case statements are pretty much like if statements, except the syntax is a little different:

switch (statement) {
case value1 :
//actions
break;
}


Switch is the variable that you will be defining and value1 is what you want your variable equal to do the following actions.


You can also use multiple case statements in a swtich statement, and just like the if/else, you have the default statement:

switch (statement) {
case value1 :
//actions
break;
case value3 :
//actions
break;
default :
//actions
break;
}


If the variable equals value1, do actions but if the variable equals value2 do actions but if the variable equals value3 do actions otherwise do actions. Get it?

One more thing about switch/case statements, YOU MUST put the break statement at the end, or else the code will run through each case statement and do all of the actions, creating a big BIG mess of things.


And finally, we have ?/: statements.

They work the same, except the ? is used as an if statement and the : is used as the else statement:

statemnt ? //actions : //actions


You can also put multiple statements together, like so:

statement1 ? //actions : statement2 ? //actions : //actions


Two more things on ?/: statements, if you don't want any actions after the : then put null; after it, and if you want to declare multiple actions after the ? or the : you must put the in brackets and seperate them with a comma, like so:

statement ? (//actions, //actions) : (//actions, //actions)



Here's a few examples incase you didn't quite get all of that:

if/else statement:

if (variable == 100) {
trace("variable equals 100");
} else if (variable == 200) {
trace("variable equals 200");
} else {
trace("variable doesn't equal 100 or 200");
}



switch/case statement:

switch (variable) {
case 100 :
trace("variable equals 100");
break;
case 200 :
trace("variable equals 200");
break;
default :
trace("variable doesn't equal 100 or 200");
break;
}



?/: statement:

variable == 100 ? trace("variable equals 100") : variable == 200 ? (trace("variable doesn't equal 100:"), trace("it equals 200")) : null;


Well, that's about it. Hope this helped! :D


Posted By mikeMarek
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
 
VBAssassin     Posted 3.99 Years Ago
 
 
Two things...

?/: are called "ternary operators".

And if you use [code][/code] tags it will keep the indents to your code snippets
;-) you can also add a title to each code snippet like this: [code][title]title goes
here[/title]code goes here[/code]

Apart from that i agree with cinjection, a nice intro ;-)

Kind regards,
Scott
 
Cinjection     Posted 3.99 Years Ago
 
 
Nice intro to selection statements. :)
Page 1 of 1
More Articles By This Author
[PHP] Create A Unique Page Hits Counter
Actionscript Events
Actionscript API
if/else, switch/case and ?/: Statements In Actionscript 2.0
Recently Posted "Programming" Articles
thetrojan01's brainfuck tutorial - from the beginning to the interpreter.
My Experiences So Far with Languages
Selecting Your First Programming Language
[PHP] - Lets kill the complex IF
Basic PortScanner in VB6.0
N-Queens-Series Part I (Only the fittest survive)
Currying in JavaScript: Fun for the Whole Family!
[PHP] Create A Unique Page Hits Counter
Actionscript Events
Actionscript API
Recently Rated "Programming" Articles
thetrojan01's brainfuck tutorial - from the beginning to the interpreter.
[PHP] - Lets kill the complex IF
Actionscript API
Actionscript Events
My Experiences So Far with Languages
Selecting Your First Programming Language
Vectors in C++
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Hardware (1)
Internet & Web Sites (3)
Life In General (2)
Networking (1)
Operating Systems (4)
Other (2)
Programming (46)
Security (8)
Software Development (5)
Web Development (14)
search Search Inside
Programming
 
 
Latest News About Coder Profile
Coder Profile Poll
Why do you get bored with programming?

Not enough time to do something productive
I run out of ideas
Too hard to show people my creations
Everything i do has too many errors, and it's too hard
I don't get bored!!!


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.20 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved