Flash / Flex / ActionScript/Language/Logical Operators

Материал из Web эксперт
Перейти к: навигация, поиск

Add extra parentheses to make the logic more apparent

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var current:Date = new Date(  );
        
        if ((current.getDate(  ) == 17) && (current.getMonth(  ) == 3)) {
          trace ("Happy Birthday, Bruce!");
        }
    }
  }
}



Logical AND

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var x = 100;
        var y = 51;
        
        if (x>50 && y>50) {
          // Code here executes only if x and y are greater than 50
        }
    }
  }
}



Logical NOT

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var now = new Date(  );        // Create a new Date object
        var day = now.getDate(  );     // Returns an integer between 1 and 31
        var month = now.getMonth(  );  // Returns an integer between 0 and 11
        
        if (  !( (month + day)==1)  ) {
          // Execute "not-January 1st" code
        }
    }
  }
}



Logical Operators in Actionscript

 
Operator      Name 
&&            And 
||            Or 
!             Not 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sUsername:String = "Joe";
        var sPassword:String = "oass";
        
        if (sUsername == "Joey"){
          if (sPassword == "isAwesome"){
            trace("That is the correct username and password.");
          }
        }
        if (sUsername == "Joey" && sPassword == "isAwesome"){
          trace("That is the correct username and password.");
        }
    }
  }
}



Logical OR

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var x = 10;
        var y = 15;
        if (x || y) {
          // This code executes if one of either x or y is not zero
        }
    }
  }
}



The logical NOT operator is often used in compound conditions with the logical OR operator

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var userName:String = "Bruce";
        
        if (!((userName == "Bruce") || (userName == "Joey"))) {
          trace ("Sorry, but only Bruce and Joey have access to this application.");
        }
   
    }
  }
}



Use a logical NOT operator, !, to check if a condition is not true

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var userName:String = "Bruce";
        if (!(userName == "Bruce")) {
          trace ("This application knows only Bruce"s birthday.");
        }
    }
  }
}



Use the inequality operator, !=

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var userName:String = "Bruce";
        
        if (userName != "Bruce") {
          trace ("This application knows only Bruce"s birthday.");
        }
    }
  }
}



Use the logical OR operator, ||, to test whether either condition is true

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var current:Date = new Date(  );
        
        if ((current.getDay(  ) == 0) || (current.getDay(  ) == 6) ) {
          trace ("Why are you working on a weekend?");
        }
    }
  }
}