Flash / Flex / ActionScript/Language/Logical Operators

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

Add extra parentheses to make the logic more apparent

   <source lang="java">

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!");
       }
   }
 }

}

       </source>
   
  


Logical AND

   <source lang="java">

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
       }
   }
 }

}

       </source>
   
  


Logical NOT

   <source lang="java">

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
       }
   }
 }

}

       </source>
   
  


Logical Operators in Actionscript

   <source lang="java">

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.");
       }
   }
 }

}

       </source>
   
  


Logical OR

   <source lang="java">

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
       }
   }
 }

}

       </source>
   
  


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

   <source lang="java">

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.");
       }
  
   }
 }

}

       </source>
   
  


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

   <source lang="java">

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.");
       }
   }
 }

}

       </source>
   
  


Use the inequality operator, !=

   <source lang="java">

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.");
       }
   }
 }

}

       </source>
   
  


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

   <source lang="java">

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?");
       }
   }
 }

}

       </source>