Operators

Filed under , on Thursday, February 18, 2010

0

What is Operators
Operators are symbols that act upon data.They represent a particular operation to be performed on the data. The operations performed are:
Mathematical:
o    Add
o    SubtractOperator
o    Multiply
o    Divide
o    Exponentiation
o    Modulus
Comparison:
o    Greater than (>)
o    Less than (<)
o    Greater than or equal to (>=)
o    Less than or equal to (<=)
o    Equal to (= (Basic style) or = = (C style))
o    Not equal to (<>(Basic style) or != (C style))
Logical
o    AND
o    OR
o    NOT
    Consider the following line:
    9 + 2
The above line indicates that the two numbers, 9 and 2, are to be added.
The ‘+’ symbol used in the above example indicates the operation that is to be performed on the two numbers. It is referred to as an operator.
Thus, operators specify the action that is to be performed on the given data.
‘9’ and ‘2’, the numbers to be added are called the operands.
Thus, an operand is the data on which the operator acts upon.



Classification  
Depending on the nature of operation, operators and classified as:
o        Arithmetic operators
o        Comparison operators
o        Logical operators
  
Arithmetic Operators
Operators that denote the mathematical operations are:
o        Add
o        Subtract
o        Multiply
o        Divide
o        Exponentiation
These are referred to as arithmetic operators.
The following table presents arithmetic operators we have used all our lives:

  
+    Addition,    9+2 = 11
-    Subtraction,    9-2 = 7
/    Division,   9/2 = 4.5
*    Multiplication,    9*2 = 18
^    Exponentiation,    9^2 = 81

We are familiar with the first four operations. Let us consider an example for fifth operator.
9^2
The number to the right of ‘^’ that is, 2 is the exponent and is the number of times the number to the left of ‘^’ that is, 9 is to be multiplied. The result is 9*9 = 81.
Another example is:
5^3
This effectively means:
5*5*5
Thus,
5^3 = 5*5*5 = 125
Below are other Arithmetic operators:

Operator  Function  Example  Result 
- Negation-(9+2)-11
MOD Modulus9 mod 21
 -(9+2) stands for ‘minus (9+2)’  or a negative value of the result of addition between 9 and 2.
MOD operator returns the remainder of a division.  

For example:
9 MOD 2
Will result in ‘1’.
Similarly,
2 MOD 4
will evaluate to 2. This is so because 2/4 means a quotient of 0 and a remainder of 2 while the result of
4 MOD 2
will also evaluate to 0 as 4 is wholly divisible by 2.
  
Precedence for Arithmetic Operators
When there are multiple operators involved in a calculation there is a certain order in which the operators are applied.
    For example, consider the following equation:
    4+3*6/2-5+5
    To solve this equation, we have to apply the operators in a certain order.

Precedence  Operator  Function 
1 -Negation
2 ^Exponentiation
3 *,/ Multiplication, Division
4 +,-  Addition, Subtraction
5  MODModulus

 From table above, it can be seen that some operators have the same precedence. When two operators of the same precedence occur they are solved by moving through the equation from the left to right.
    4+3*6/2-5+5
is solved as follows:
•    4+ [3*6/2] -5+5 (Of the different operators used, ‘*’ and ‘/’ have the highest precedence)
•    4+ [18/2] -5+5  (‘*’ and ‘/’ have the same precedence. Operators of the same precedence are resolved by moving through the equation from left to right)
•    [4+9]-5+5  (‘+’ and ‘-‘ have the same precedence. Operators of the same precedence are resolved by moving through the equation left to right)
•    [13-5] + 5
•    8+5
•    13

Comparison Operators  
There are several operators that are used to compare two values on the basis of a certain condition. These operators are called Comparison Operators.
Comparison operators return either ‘True’ or ‘False’.
For example, the result of the comparison:
Is 2 ‘greater than’ 9?
Is ‘False’ because 2 is not greater in value than 9. On the other hand, the result of the comparison:
Is 9 ‘greater than’ 2?
Is ‘True’ because 9 is greater in value that 2.
Table below presents the comparison operators:

Operator    Functionality    Example    Result
<    Less than    2<9    True
        9<2    False
        2<2    False
<=    Less than or Equal to    2<=9    True
        9<=2    False
        2<=2    True
>    Greater than    2>9    False
        9>2    True
        2>2    False
>=    Greater than or Equal to     2>=9    False
        9>=2    True
        2>=2    True
=    Equal to    2=9    False
        9=2    False
        2=2    True
<>    Not equal to    2<>9    True
        9<>2    True
        2<>2    False
  
The comparison operators compare the operand on the left with the operand on the right and evaluate to either ‘True’ or ‘False’.
For example,
    2>9
    checks if 2 is greater than 9. Since, it 2 is not greater than 9 the answer is False.
    Comparison operators determine if a particular condition is satisfied. However, there are situations where:
        Multiple conditions need to be satisfied
        One of the many conditions is to be satisfied
    In such situations, we need operators that combine result of several comparisons, as required, to present a single answer.
Logical Operators
  
The logical operators may also result in ‘True’ and ‘False’ values.
Table below provides a listing of the logical operators.

Operator    Function
AND    Result is ‘True’ when both conditions are ‘True’
OR    Result is ‘True; when either of the two conditions is ‘True’
NOT    Operates on a single value and converts ‘True’ to ‘False’ and vice-versa
  
This section to follow describe the use of the various logical operators.
AND Operator  
    Table below presents the result of using the AND operator.
  
Condition 1    Condition 2    Condition 1 AND Condition 2
False    False    False
False    True    False
True    False    False
True    True    True
  
    For example, for Gosteady Inc. to accept an order the quantity should be a minimum of 8 and the bill amount should be greater than or equal to 1000.
    This translates to two conditions:
•    Quantity  >= 8
•    Amount >= 1000
    Since, both the conditions have to evaluate to ‘True’ for the order to be accepted, we use the AND operator to combine the results of the two comparisons as shown below:
    Quantity >= 8 AND Amount >=1000
    The results of using the AND operator as shown above are shown in table below.
  
Quantity    Amount    Quantity
>=8    Amount
>=1000    (Quantity>=8)
AND
(Amount >=1000)
2    900    False    False    False
2    1400    False    True    False
9    600    True    False    False
8    2000    True    True    True

OR Operator  
Table below presents the result of using the OR operator.
  
Condition 1    Condition 2    Condition 1 OR Condition 2
False    False    False
False    True    True
True    False    True
True    True    True
  
    For example, for Gosteady Inc. to accept an order the quantity should be a minimum of 8 or the bill amount should be greater than or equal to 1000.
  
    This translates to two conditions:
        Quantity>=8
        Amount>=1000
    For the order to be accepted, either of these conditions have to be ‘True’ and so we use the OR operator to join them as shown below:
    Quantity >= 8 OR Amount >= 1000
Not Operator  
The NOT operator uses a single operand.
Table below presents the results of using the NOT operator.
  
Condition    NOT Condition
False    True
True    False
  
    For example, Gosteady Inc. will not accept an order with quantity less than 8.
    In terms of the NOT operator this condition is represented as:
    NOT (Quantity < 8)
  
Precedence for Logical Operators
Table below presents the order of precedence for logical operators.
  
Precedence    Operator
1    NOT
2    AND
3    OR
    When multiple instances of a logical operator are used in a condition, they are evaluated from right to left.
    For example, consider the following condition:
    False OR True AND NOT False AND True
    This condition gets evaluated as shown below:
        False OR True AND [NOT False] AND True
    NOT has the highest precedence.
        False OR True AND [True AND True]
    Here, AND is the operator of the highest precedence and operators of the same precedence are evaluated from right to left.
        False OR [True AND True]
        [False OR True]
        True
  
Precedence Among the Different Types of Operators
When an equation uses more than one type of operator than the order of precedence has to be established with the different types of operators.
Table below presents the precedence among the different types of operators.
  
Precedence    Type of Operator
1    Arithmetic
2    Comparison
3    Logical
  
    Thus, in an equation involving all three types of operators, the arithmetic operators are evaluated first followed by comparison and then logical. The precedence of operators within a particular type has been defined earlier.
    Consider the following example:
    2*3+4/2 > AND 3<5 OR 10<9
    The evaluation is as shown:
        [2*3+4/2] > 3 AND 3<5 OR 10<9
    First the arithmetic operators are dealt with. The order of precedence for the evaluation arithmetic operators is as in table before.
        [[2*3]+[4/2]] > 3 AND 3<5 OR 10<9
        [6+2]>3 AND 3<5 OR 10<9
        [8>3] AND [3<5] OR [10<9]
Next to be evaluated are the comparison operators all of which have the same precedence and so are evaluated from left to right.
        True AND True OR False
    The last to be evaluated are the logical operators. AND takes precedence over OR.
        [True AND True] OR False
        True OR False
        True
The Parentheses  
The precedence of operators can be modified by using the parentheses to specify which part of the equation is to be solved first.
        When there are parentheses within parentheses the innermost parentheses are to be evaluated first.
        When an equation involves multiple sets of parentheses, they are evaluated left to right.
    Consider the following example:
    5+9*3^2-4 > 10 AND (2+2^4-8/4 > 6 OR (2<6 AND 10>11))
    The solution is:
        5+9*3^2-4 > 10 AND (2+2^4-8/4 > 6 OR (True AND False))
    The inner parentheses takes precedence over all other operators and the evaluation within this is as per the regular conventions defined in Precedence table before.
        5+9*3^2-4 > 10 AND (2+2^4-8/4 > 6 OR False)
        5+9*3^2-4 > 10 AND (2+16-8/4 > 6 OR False)
    5+9*3^2-4 > 10 AND (2+16-2 > 6 OR False)
        5+9*3^2-4 > 10 AND (18-2 > 6 OR False)
        5+9*3^2-4 > 10 AND (16 > 6 OR False)
        5+9*3^2-4 > 10 AND (True OR False)
        5+9*3^2-4 > 10 AND True
        5+9*9-4 > 10 AND True  
    The expression to the left is evaluated as per the conventions.
        5+81-4 > 10 AND True  
        86-4 > 10 AND True  
        82>10 AND True
        True AND True
        True
    Thus, the result from expression above is True.

Assignment  
  Every expression always give result values. Thus, we use variables to save these values. Storing values into variables are called Assignment operation. In most Programming Tools, one assignment operation can store value to only one variable. Assignment operation has three parts: left-side, ‘=’ operator and right-side. Left-side part is variable we want to store value to, and right-side part is expression. Below are the examples to assign variables with values:

    1. Num = 5*2+7/2                 correct
    2. Num = 5*2+7/2>10                 correct
    3. Num = 5*2=10 AND 10>18 OR 11<3+4     correct
    4. Num, Num2 = 10+2 AND 8+2         incorrect!
    5. 201 = 5*2+7/2 AND 6+4             incorrect!
    Line 4 and 5 are incorrect assignment operations. From line 4 and 5, we know multiple variables can not be assigned and we must use variable for the left-side part of assignment.
    We also can use variables in expression. Note that only numeric types can be used in arithmetic operations. Alphanumeric types are converted automatically to numeric types for several programming tools, while most of programming tool do not convert any Character or Alphanumeric types when dealt with arithmetic operation. Study expression below:
  
    1. Num = 1             ‘First, store 1 to Num
    2. Total = 10*(Num+5)/6    ‘What is Total’s value?
    The solution is:
        Total = 10 * (Num +5) / 6
    The inner parentheses takes precedence over all other operators and the evaluation within this is as per the regular conventions defined in Precedence table before. Value of Num is 1, so we can substitute Num with 1.
        Total = 10 * (1 + 5) / 6
        Total = 10 * 6/6
        Total = 10 * 1
        Total = 10
    Thus, 10 is stored into Total.

Comments Posted (0)

Post a Comment