• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 7. Conditionals > The switch Statement

7.4. The switch Statement

The switch statement, introduced in Flash Player 6, lets us execute one (or sometimes more) of several possible code blocks, based on the value of a single test expression. In some cases, the switch statement is easier to write and more legible than an if statement. For example, in the following switch statement, we greet the user with a custom message depending on the value of the test expression gender:

var surname = "Porter";
var gender = "male";
switch (gender) {
  case "femaleMarried":
    trace("Hello Mrs. " + surname); 
    break;
  case "femaleGeneric":
    trace("Hello Ms. " + surname);
    break;
  case "male":
    trace("Hello Mr. " + surname);
    break;
  default:
    trace("Hello " + surname);
}


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint