123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computers >> View Article

Action Script 3.0: The Switch Statement

Profile Picture
By Author: Bruce Tucker
Total Articles: 174
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

The switch statement is very common in many programming languages. It VB.net is called a select-case, but regardless of what it is called the process is the same. You feed the statement a value called an expression. The statement then will execute a branch of code based on the value of that expression. Sounds simple right? Good let's get started with the actual code.

Your first line of code sets up your switch statement. It is written as:

Switch (expression) {

You start using the keyword Switch then you use an open parenthesis (, followed by the expression you want to evaluate, then we type in a close parenthesis ) and then we use the open curly brace to begin our code block {.

Your expression can be pretty much anything you want so as long as the Action Script compiler can determine what it is you want to do. Let's say for example you want to display a certain image on a webpage based on some user selection. There are a number of images to choose from therefore making the switch statement a perfect use here.

For our example lets say there are 5 images. Now you could write various ...
... nested if-else statements but that would be too cumbersome. Instead you write the switch statement and branch off to display the image in what is called the Case code block. The keyword Case determined whether or not the code underneath of it will run based on the value of the expression. So in our example our code would look like this:

Switch (ImageChoice) {
Case 1:
// Insert your code to display the image
Break;

The characters // represent comments in your code and do not get executed. They are ignored by the compiler.

The above code says that if the user chose image 1 then you are going to run the code that immediately follows Case 1. Notice the code break;. That is very important, because if you do not put that in, the program will execute the code under case 1, and each following case thereafter until either it hits a break or the end of the switch statement.

So far we have one case, but remember we have 5 for our example. Therefore we would write the following code:

Switch (ImageChoice) {
Case 1:
// Insert your code to display the image
Break;
Case 2:
// Insert your code to display the image
Break;
Case 3:
// Insert your code to display the image
Break;
Case 4:
// Insert your code to display the image
Break;
Case 5:
// Insert your code to display the image
Break;

As you can see depending on the value of ImageChoice will determine which branch of code will be executed.

What would happen in the code above if somehow the value of ImageChoice was 6? Well in the code above nothing would happen because there is no branch for the value of 6. The switch statement provides a nice catch all called default. And what this does is execute code if the value of the expression does not match and of the case statements. So lets say in our example it is possible for ImageChoice to be a number from 1 through 100, however we only want to show an image if the number is 1 through 5. Any other number will display a generic image. Our code would look like this:

Switch (ImageChoice) {
Case 1:
// Insert your code to display the image
Break;
Case 2:
// Insert your code to display the image
Break;
Case 3:
// Insert your code to display the image
Break;
Case 4:
// Insert your code to display the image
Break;
Case 5:
// Insert your code to display the image
Break;
Default:
//Insert your code to display the default image

You now have caught anything else that might occur that you weren't prepared for. I recommend to always include a default statement at the end of every switch statement to insure against unforeseen issues.

You aren't quite done yet though. Remember you opened the switch statement's code block with an open curly brace {. Now that we have reached the end of the switch statement we need to close it with the corresponding closed curly brace }. Our final code looks like this:

Switch (ImageChoice) {
Case 1:
// Insert your code to display the image
Break;
Case 2:
// Insert your code to display the image
Break;
Case 3:
// Insert your code to display the image
Break;
Case 4:
// Insert your code to display the image
Break;
Case 5:
// Insert your code to display the image
Break;
Default:
//Insert your code to display the default image
}

And there you have it a complete switch statement that you can use in your next Action Script program.
About the Author:
Mike is an ambassador for the document database Oboulo. Oboulo is a publicly authored, publicly accessible database covering a wide array of topics. Each time you publish a paper, article, or presentation on Oboulo using Ambassador number 52a141, you will receive $15.00 in royalties.

Total Views: 407Word Count: 816See All articles From Author

Add Comment

Computers Articles

1. Exploring How Ai In The Cloud Can Transform Your Business
Author: TechDogs

2. The Power Of Cloud And Ai: A New Era Of Collaboration
Author: TechDogs

3. Get Business Insights Using Expedia & Booking. Com Review Data Scraping
Author: DataZivot

4. Top 10 Reasons A Strong Communication Strategy Drives Prm Program Success
Author: Archi

5. Achieve Scalable Web Scraping With Aws Lambda
Author: Devil Brown

6. Overcoming Common Challenges In Iso 27001 Implementation
Author: Jenna Miller

7. Basic Computer Course: Your Gateway To Skill Development | The Institute Of Professional Accountants
Author: Tipa Institute

8. Top 7 Advantages Of React Js
Author: Bella Stone

9. Top 7 App Marketing Tools For Mobile Success
Author: Bella Stone

10. Revolutionizing Education Management With Samphire It Solution Pvt Ltd’s Erp Software
Author: CONTENT EDITOR FOR SAMPHIRE IT SOLUTIONS PVT LTD

11. Top 10 Healthcare Technology Trends
Author: goodcoders

12. "building Tomorrow’s Factories: The Role Of Automation & Robotics In Modern Manufacturing"
Author: andrew smith

13. The Ultimate Guide To The Best Ecommerce Plugin For Wordpress
Author: Rocket Press

14. Xsosys Erp: A Scalable Solution For Businesses In Any Industry
Author: Xsosys Technology(S) Pte. Ltd.

15. Rental Management Software: A Complete Solution For Car, Property, And Coworking Space
Author: RentAAA

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: