Flash / Flex / ActionScript/Language/Arithmetic Operators

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

Arithmetic Operators in Actionscript

 
Operator     Name                Example     
+            Plus                x + y       
-            Minus/Negation      x - y       
*            Multiply            x*y       
/            Divide              x/y       
%            Modulo              x%y       
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var nYear:Number = 2004;
        trace(nYear % 4); // Displays: 0
        
    }
  }
}



Assignment Operators

 
  
Operator      Name      Example      What It Means 
=       Equals (assignment)      x = y      x = y 
+=      Add by value      x += y      x = x + y 
-=      Subtract by value      x -= y      x = x - y 
*=      Multiply by value      x *= y      x = x * y 
/=      Divide by value      x /= y      x = x / y 
%=      Modulo by value      x %= y      x = x % y 
<<=      Left shift by value      x <<= y      x = x << y 
>>=      Right shift by value      x >>= y      x = x >> y 
>>>=      Right shift zero fill by value      x >>>= y      x = x >>> y 
&=      Bitwise AND by value      x &= y      x = x & y 
|=      Bitwise OR by value      x |= y      x = x | y 
^=      Bitwise XOR by value      x ^= y      x = x ^ y



Compound Assignment Operators (+=, -=, *=, /=, and %=): x += 1; is the same as writing x =

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var x:Number = 0;
        
        x += 5; // x = 5
        trace(x);
        
        x -= 3; // x = 2
        trace(x);
        x *= 3; // x = 6
        trace(x);
        x /= 2; // x = 3
        trace(x);
        x %= 3; // x = 0
        trace(x);
    }
  }
}



Increment (++) and Decrement ( -- )

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var x:Number = 5;
        x++; // 6
        trace(x);
        
        x--; // 5
        trace(x);
    }
  }
}



Modulo (%)

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var a:int = 9;
        var b:int = 3;
        
        trace(a%b);
    }
  }
}



myVariable *= 6; is the same as: myVariable = myVariable * 6;

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var myVariable:Number = 6;
        
        myVariable *= 6;
        trace(myVariable);
        
        myVariable = myVariable * 6;
        
        trace(myVariable);
        
    }
  }
}



myVariable += 6; is the shorthand version of the following: myVariable = myVariable + 6;

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var myVariable:Number = 6;
        
        myVariable = myVariable + 6;
        trace(myVariable); //12
        
        myVariable += 6;
        
        trace(myVariable); //18
    }
  }
}



Using Mathematical Operators: prefix and postfix

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var quantity:Number = 5;
        trace(quantity++);  // Displays: 5
        trace(quantity);    // Displays: 6
        
        var quantity:Number = 5;
        trace(++quantity);  // Displays: 6
        trace(quantity);    // Displays: 6
    }
  }
}