Thursday 9 April 2020

Operators


What is the operator? Explain its types.
An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation on data stored in variables. The variables that are operated are termed as operands.
Operators can be classified into a number of categories. They include:



1.         Arithmetic operators
2.         Relational operators
3.         Logical operators
4.         Assignment operator
5.         Increment and decrement operators
6.         Conditional operator
7.         Bitwise operators
8.         Special operators                    
Now, let us discuss each category in detail.

1. Arithmetic Operators
C++ provides all the basic arithmetic operators. There are five arithmetic operators in C++.

S.No.
Operator
Purpose
1
+
Addition
2
-
Subtraction
3
*
Multiplication
4
/
Division
5
%
Remainder after integer division

The division operator (/) requires the second operand as non zero, though the operands need not be integers.
The operator (%) is known as modulus operator. It produces the remainder after the division of two operands. The second operand must be non-zero.
All other operators work in their normal way.

2. Relational Operators
Relational operator is used to compare two operands to see whether they are equal to each other, unequal, or one is greater or lesser than the other.
The operands can be variables, constants or expressions, and the result is a numerical value. There are six relational operators.


S.No.
Operator
Purpose
1
= =
equal to
2
! =
not equal to
3
< 
less than
4
< =
less than or equal to
5
> 
greater than
6
> =
greater than or equal to
TableRelational Operators

A simple relation contains only one relational expression and takes the following form:
ae-1 relational operator ae-2
ae-1 and ae-2 are arithmetic expressions, which may be simple constants, variables or a combination of these. The value of the relational operator is either 1 or 0. If the relation is true, the result is 1 otherwise it is 0.
Example


S.No.
Expressions
Result
1
4.5 < = 10
True
2
4.5 < -10
False
3
-35 > = 0
False
4
10 < 7+5
True
Table: Example of Relational Operator

3. Logical Operators
Logical operators are used to combining two or more relational expressions. There are three logical operators.

S.No.
Operator
Meaning
1
&&
Logical and
2
¦¦
Logical or
3
!
Logical not
Table: Logical Operators

The result of Logical AND will be true only if both operands are true. While the result of a Logical OR operation will be true if either operand is true. Logical NOT (!) is used for reversing the value of the expression.
The expression which combines two or more relational expressions is termed as Logical Expression or Compound Relational Expression which yields either 1 or 0.
Example:         
1.  if (age > 50 && weight < 80)
2.  if ( a < 0 ¦ ¦ ch = = 'a')
3.  if (! (a < 0))

4. Assignment Operators
Assignment operators are used to assigning the result of an expression to a variable. The most commonly used assignment operator is (=). Note that it is different from mathematical equality.
An expression with assignment operator is of the following form:
identifier = expression
The shorthand assignment operators are of following type:
v op = exp;
This is equivalent to    v = v op exp;
Example:
            i = i+5; is equivalent to  i + = 5;
    i = i * (y+1)     is equivalent to i * = (y+1)

The advantages of using assignment operators are:
1.         The statement is more efficient and easier to read.
2.         What appears on the L.H.S need not to be repeated and therefore it becomes easier to write for long variable names.

Example.:    # include <iostream.h>
                            main( )
                            {
                                        int averylongvariablename;
                                        averylongvariablename = 2;
                                        while (averylongvariablename < 20)
                                        {
                                                    Cout<<" averylongvariablename”;
                       
                        averylongvariablename*= averylongvariablename;
                       
                                        }
                            }
Output:                          
2
4
16



No comments:

Post a Comment