Flash / Flex / ActionScript/Statement/switch
Содержание
- 1 Adding a break statement to switch statement
- 2 Adding a default statement
- 3 switch statement executes one of several possible code blocks based on the value of a single test expression
- 4 The general form of a switch statement
- 5 The switch statement is useful when performing the same action for one of several matching possibilities.
- 6 Working with the switch Statement
Adding a break statement to switch statement
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var myVariable:Number = 6;
switch(myVariable){
case 10:
trace("10");
break;
case 6:
trace("6");
break;
case 1:
trace("1");
}
}
}
}
6
Adding a default statement
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var myVariable:Number = 6;
switch(myVariable){
case 10:
trace("10");
break;
case 6:
trace("6");
break;
case 1:
trace("1");
break;
default:
trace("none of the cases were met");
}
}
}
}
switch statement executes one of several possible code blocks based on the value of a single test expression
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var greeting;
var language = "english";
switch (language) {
case "english":
greeting = "Hello";
break;
case "japanese":
greeting = "Konnichiwa";
break;
case "french":
greeting = "Bonjour";
break;
case "german":
greeting = "Guten tag";
break;
default:
// Code here (not shown) would display an error message indicating
// that the language was not set properly
}
trace(greeting);
}
}
}
The general form of a switch statement
switch (testExpression) {
case caseExpression:
// case body
case caseExpression:
// case body
default:
// case body
}
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var animalName:String = "dove";
switch (animalName) {
case "turtle":
trace("Yay! "Turtle" is the correct answer.");
case "dove":
trace("Sorry, a dove is a bird, not a reptile.");
default:
trace("Sorry, try again.");
}
}
}
}
The switch statement is useful when performing the same action for one of several matching possibilities.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var animalName:String = "dove";
switch (animalName) {
case "turtle":
case "iguana":
trace("Yay! You named a reptile.");
break;
case "dove":
case "cardinal":
trace("Sorry, you specified a bird, not a reptile.");
break;
default:
trace("Sorry, try again.");
}
}
}
}
Working with the switch Statement
//The basic structure of a switch statement is:
/*
switch(expression){
case testExpression:
statement;
[case testExpression2:
statement;
default:
statement;]
}
*/
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var myVariable:Number = 6;
switch(myVariable){
case 10:
trace("10");
case 6:
trace("6");
case 1:
trace("1");
}
}
}
}
6
1