Flash / Flex / ActionScript/Statement/if
Содержание
An if statement with no else
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
if (1 > 10) {
trace(1>10);
}
}
}
}
Chaining if statements
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var greeting;
var language = "english";
if (language == "english") {
greeting = "Hello";
} else if (language == "japanese") {
greeting = "Konnichiwa";
} else if (language == "french") {
greeting = "Bonjour";
} else if (language == "german") {
greeting = "Guten tag";
} else {
}
trace(greeting);
}
}
}
If Statements
if (logical expression) {
// code to execute if the expression is true
}
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var weather:String = "rain";
if (weather == "rain") {
trace("bringUmbrella");
}
}
}
}
If Statement with boolean constant
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var greeting;
if (true) {
greeting = "Hello";
} else {
greeting = "Bonjour";
}
trace(greeting);
}
}
}
If statement with else
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var animalName:String = "turtle";
if (animalName == "turtle") {
trace("Yay! "Turtle" is the correct answer.");
}
else {
trace("Sorry, you got the question wrong.");
}
}
}
}
Performing Actions Conditionally
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var animalName:String = "turtle";
if (animalName == "turtle") {
trace("Yay! "Turtle" is the correct answer.");
}
}
}
}
Performing Complex Conditional Testing
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
// Use the logical AND (&&), OR (||), and NOT (!) operators to create compound conditional statements.
var current:Date = new Date( );
if (current.getDate( ) == 17 && current.getMonth( ) == 3) {
trace ("Happy Birthday, Bruce!");
}
}
}
}