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 241 Days 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 242 Days 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
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
Quick Threading Tutorial - C++
C++ And Me (A Love Story)
First look at C
Introduction to Pseudo Code
Integrating your website with PHP
Recently Rated "Programming" Articles
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
N-Queens-Series Part I (Only the fittest survive)
[PHP] Create A Unique Page Hits Counter
Currying in JavaScript: Fun for the Whole Family!
Actionscript Events
First look at C
if/else, switch/case and ?/: Statements In Actionscript 2.0
Actionscript API
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Operating Systems (3)
Other (2)
Programming (48)
Security (10)
Software Development (5)
Web Development (15)
search Search Inside
Programming
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved