Now that we have learnt about data, we can now learn about operators. Operators manipulate or compare one or two data values. Simple examples are the +, -, / and * operators which add, subtract, divide and multiply two data values.

There are two types of operators - Binary and Unary

Binary Operators


Operators that manipulate on two data values are called binary operators. There are different types -

Arithmetic Operator

These modify the values of variables by using mathematical operations.

Name and Symbol
Form
Explanation
Return Value
Addition ( + )
a+b
Adds the value of a and b together
If both are Int, it returns Int.
If one is Float and the other is either Float or Int then it returns Float.
If one is String and the other is String or any other value it returns String.
Subtract ( - )
a-b
Subtracts value of b from a
If both are Int it returns Int. If one of them is Float it returns Float.
Multiply ( * )
a*b
Multiplies value of a and b together
If both are Int it returns Int. If one of them is Float it returns Float.
Divide ( / )
a/b
Divides the value of a by value of b
Always returns Float
Modulus ( % )
a%b
Returns the modulo of the values a
and b ,i.e, divides a/b and returns the
remainder
If both are Int it returns Int. If one of them is Float it returns Float.

Bitwise Operator


These perform bitwise calculations on the two Int variables.

Name and Symbol
Form
Explanation
Return Value
Bitwise AND ( & )
a&b
Performs bitwise AND on and a and b
Returns an Int.
Bitwise OR ( | )
a|b
Performs bitwise OR on and a and b
Returns an Int
Bitwise XOR ( ^ )
a^b
Performs Exclusive OR bitwise operation
on a and b
Returns an Int
Right Shift ( >> )
a>>b
Performs an arithmetic right shift on the value of
a by the value of b
Returns an Int
Left Shift ( << )
a<<b
Performs an arithmetic left shift on the value of
a by the value of b.
Returns an Int
Logical Right Shift ( >>>)
a>>>b
Performs a logical right shift on the value of
a by the value of b.
Returns an Int
You can learn more about Bitwise operations at Wikipedia or a Math site.

Assignment Operator


These assign the value of one variables to another.

Name and Symbol
Form
Explanation
Return Value
Assignment ( = )
a=b
Assigns the value of b to a
Returns value of b
Arithmetic and assignment
( +=, -=, *=, /=, %=, ^=, &=, |=, <<=, >>= , >>>= )
a+=b
Performs the arithmetic operation and assigns
the value to a
It will return the value of the expression on the left hand side

The last one is basically a shorthand. For example, instead of writing a = a+b we can use a+= b. Thus, it performs the arithmetic operation and assigns the value in one expression.

Comparison Operator


These compare the values of one variable with the other variable.

Name and Symbol
Form
Explanation
Return Value
Equality ( == )
a==b
Check to see if a has the same value
as b
Returns true (a boolean value) if a is equal to b
else it returns false ( a boolean value)
Inequality ( !=)
a!=b
Check to see if a does not have the
same value as b
Returns true if a is not equal to b
else it returns true
Greater Than ( > )
a>b
Check to see if a is greater
than b
Returns true if a is greater than
b else it returns false
Greater Than
or Equal to ( >= )
a>=b
Check to see if a is greater than
or equal to b
Returns true if a is greater than
or equal to b else it returns false
Less Than ( < )
a<b
Check to see if a is less than b
Returns true if a is less than b
else returns false
Lesser Than
or Equal To ( <= )
a<=b
Check to see if a less than
or equal to b
Return true if a is less than or equal to b
else returns false

Boolean Operator


These perform an operation on two booleans based on Boolean algebra.

Name and Symbol
Form
Explanation
Return Value
Logical AND ( && )
a&&b
Performs logical AND operation
on a and b. a and b should be boolean
Returns true or false according to boolean arithmetic
Logical OR
( | | )
a| |b
Performs logical OR operationon a and b. a and b should be boolean
Returns true or false according to boolean arithmetic


Logical And and Logical Or return true or false depending on the values of a and b.

Logical AND


  • If a is true and b is true then return value is true

  • If a is true and b is false then return value is false

  • If a is false and b is true then return value is false

  • If a is false and b is false then return value is false

Basically, both a and b have to be true for the logical AND to evaluate to true.

Logical OR


  • If a is true and b is true then return value is true

  • If a is true and b is false then return value is true

  • If a is false and b is true then return value is true

  • If a is false and b is false then return value is false

Basically, either a or b or both can be true for it to evaluate to true.

All this comes under Boolean algebra and you might want to check it out in Wikipedia or in a Math site.

Unary Operators


Operators that manipulate on only one data value are called unary operators.

Name and Symbol
Form
Explanation
Return Value
Not ( ! )
!a
Inverses value of
a. a should be Boolean
Returns the inverse of thevalue a.
Negation ( - )
-a
Negates a.
a should be Int or Float
Returns negative value ofa
Post Increment ( ++ )
a++
Increments value of a
by 1
Returns the value of a
before incrementing it
Pre Increment ( ++ )
++a
Increment value of a
by 1
Returns the value of a
after incrementing it
Post Decrement ( -- )
a--
Decrements value of a
by 1
Returns the value of a
before decrementing it
Pre Decrement ( -- )
--a
Decrements value of a
by 1
Returns the value of a
after decrementing it

Differences Between Post and Pre


See this program -
class Test
{
 
  public static function main()
  {
    var a : Int = 0;
    var b : Int = 0;
 
    trace(a++); // Prints 0 ( value of a before incremnting ) and then increments the value of a to 1
    trace(++b); // Increments the value of b to 1 and then Prints 1 ( value of b after incrementing )
  }
 
}
This demonstrates difference between post and pre increment. Same can be shown for post and pre decrement.