Wednesday, 11 June 2014

3.0 OPERATORS AND OPERANDS



An operator is a high-level computer language specifying an operation to be performed that yields a value. An operand on the other hand is an entity on which an operator acts. In an expression

                        A  +  B

The sign + is an operator specifying the operation of addition on two operands A and B.

C++ language operators are classified into four types.

                                i.      Arithmetic operators
                              ii.      Relational operators
                            iii.      Logical/Boolean operators
                            iv.      Assignment operators

They can be further categorized as:

                                i.      Unary operator       -       Requiring one operand
                              ii.      Binary operator      -       Requiring two operands
                            iii.      Ternary operator    -       Requiring three operands

Operators can associate operators are, increment and decrement operators and conditional operators.


3.1 Arithmetic Operators

Arithmetic operators are those that perform an arithmetic (numeric) operations.


3.2 Unary Operators

 
Unary arithmetic operators need only one operand for example +a, –b, namely                  – (negative) or +(positive).

 
3.3   Binary Operators
Binary arithmetic operators requires two operands and these include
            + (addition)
            - (subtraction or minus)
            + (multiplication)
            / (division)
            % (modulus)  - For reminder and it is applied only when both operands are
 integers.

3.4   Ternary Operators

Ternary arithmetic operators take three operands. For example, the assignment:

          max = a > b ? a : b;

It means that if a > b then max is equal to aotherwise max is equal to b


The hierarchy of arithmetic operators is such that

*  /  %  are evaluated from left to right
+  -       are evaluated from left to right

If parentheses are involved then, expression within parentheses are evaluated first following the hierarchy given above. In the event of nested parentheses, the C++ compiler will evaluate the innermost parentheses first, then moving outwards and finally the hierarchy given is followed.  Figure 2.1 illustrate this concept.


Example 3.1

Evaluate the following expression as the C++ compiler would.
                        1 + ((3 + 1) / (8 – 4) – 5)

Solution

1 + ((3 + 1) / (8 – 4) – 5)          Û 1 + (4 / (8 – 4) – 5)
                                                 Û    1 +  (4 / 4 – 5)
                                                 Û      1 + (1 – 5)
                                                 Û        1 +  -4
                                                 Û          -3



3.5   Relational Operators

Relational operators are used to test the relation between two values. All C++ relational operators are binary operators and hence require two operands. A relation expression is made up of two arithmetic expressions connected by a relation operator. It returns a zero when the relation is FALSE and a non-zero when a relation is TRUE. The table below summarizes relational operators. The relational operators are summarized in table 5




Table 5:  Relational operators

Operator
Symbol
Form
Result
Grater than
> 
a > b
1 if a is grater than b, else 0
Less than
< 
a < b
1 if a is less than b, else 0
Greater than or equal to
>=
a >= b
1 if a is grater than or equal to  b, else 0
Less than or
equal to
<=
a <= b
1 if a is less than or equal to  b, else 0
Equal to
= =
a = = b
1 if a is equal to  b, else 0
Not equal to
!=
a != b
1 if a is not equal to  b, else 0

Note that all relational operators have lower precedence than the arithmetic operators.


Consider the following expression

a + b * c < d / f

This expression is evaluated as follows

            (a + (b + c)) < (d / f)


Example  3.2

Given the following declarations: 

int j = 0, m = 1, n = -1;
float x = 2.5, y = 0.0;

Evaluate the following expression

m / n < x


Solution
m / n < x     Û        (m / n) < x
                                                            Û        -1 < 2.5
                                                            Û        T = 1





 

Exercise 1


Given the declarations in example 2 above, evaluate the following expressions:

Expression                                                                 Solution
 i.     j > m                        0 (Talse)
ii.     j <= m >= n                  1 (True)
iii.     j <= x == m                  1 (True)
iv.     -x + j == y > n >= m         0 (False)
 v.     x += (y >= n)                3.5
    vi.            ++j == m != y * 2            1 (True)




3.6              Logical or Boolean Operators

A logical operator combines the result of one or more expressions and the resultant expression is called the logical expression. After testing the condition, they return logical status TRUE or FALSE as net result.

Logical operators are unary or binary operators. The operand may be constants, variables or expressions. Table 6 gives logical operators.

Table 6:   Boolean Operators
Operator
Symbol
Form
Results
Logical AND
&&
a && b
1 if a and b are non zero
else 0
Logical OR
¦¦
a¦¦ b
1 if a or b is non zero else 0
Logical negation
!
!a
1 if a is 0; else 0


In algebra, the expression
                       
x < y < z

it is true if yis greater x or less than z. In c++ however, the expression has a different meaning since it is evaluated from left to right as follows.

(x < y) < z

The result of x < y is either 0 or 1. The expression is therefore true in c++ is x is less than y and z is greater 1, or if x not less than y and z is greater than zero.


3.7   Hierarchy of Logical Operators

The logical NOT has higher precedence than the others. The AND operator (&&) has higher precedence than OR operator ( || ).  Both the logical AND and logical OR operators have lower precedence than the relational and arithmetic operators.


Exercises 2

Given the following declarations
     int j = 0, m = 1, n = -1;
     float x = 2.5, y = 0.0;
Evaluate the following expressions.                                       Solutions
a) j && m                              0
b) j < m && n < m                      1
c) m + n || !j                         1
d) x * 5 && 5 || m/n                        1
e) j <= 10 && x >= 1 && m                   1
f) !x || !n || m + n                        0
g) x * y < j + m ||n                        1
h) (x > y)| !j || n++                  1
i)        (j || m) + (x || ++n)               2



3.8   Precedence of Operators

All operators have two important properties called precedence and associatively. Operators with higher precedence have their operands bound, or grouped together before operators of lower precedence regardless the order in which they appear.  Table 7 shows the precedence of deferent operators and their associatively.

Table 7:   Precedence of Operators

Class of
Operator
Operation
Associativity
Precedence
Unary
-+

Highest
Multiplicative
*  /  %
Left-to-right
^
Additive
+  -
Left-to-right
|
Shift
<<   >>
Left-to-right
|
Relational
<   <=   >   >=
Left-to-right
|
Equality
= =   !=
Left-to-right
|
Logical END
&&
Left-to-right
|
Logical OR
¦¦
Left-to-right
|
conditional
?:
right-to-left
|
Assignment
=    +=    -=    *=
Right-to-left
|
Comma
,
Left-to-right
Lowest



Example  3.3

Given the following declarations
     int j = 0, m = 1, n = -1;
     float x = 2.5, y = 0.0;


Evaluate the following expressions   

j <= x == m

Solution:

          j <= x == m    ((j <= x) == m)
                        ((0 <= 2.5) == 1)
                           T == 1

Exercise 2

Given declarations in example 2.4 above, evaluate the following expressions:

Expression                                                                 Solutions
a)    j > m                              0        
b)    m / n < x                          1
c)    j <= m >= n                             1
d)    -x + j = = y > n >= m                   0
e)    x += (y >= n)                      3.5
f)    ++j = = m != y * 2                 1


No comments:

Post a Comment