Operators

Image result for c++ logo


C++ is very rich in built-in operators. It offers different classes of operators : arithmetic, relational, logical, increment-decrement, conditional ,etc.

Operators - The operation being carried out on data, are represented by operators.
                     ex- 5+2=7 (here + is a operator) similarly -, *, /, %, ! can also be used as operator in any operation.

Operands - The objects of the operation are referred to as operands.
                     ex- 5+2=7 (5 and 2 are the two operands used with an arithmetic operator)

Types of operators :

1. Arithmetic operators - These are used for basic arithmetic calculations such as addition, subtraction, multiplication, division, remainder which are +, -, *, / and % respectively.
-> Each of these operator is a binary operator i.e., it requires two values (operands) to calculate a final answer.
-> Here we will discuss about the working of remainder operator (%) as we all are familiar with the remaining four operators which are +, -, * and /.
* The % operator finds the modules of its first operand relative to the second i.e., it produces the remainder of dividing the first by the second operand. For example :
20 % 6 evaluates to 2, since  goes into 20 three times with a remainder 2.

2. Increment/Decrement operators (++/ --) - C++ includes two useful operators not generally found in other computer language except C.
-> The increment operator denoted by two plus sign (++) adds 1 to its operand and the decrement operator denoted by two minus sign (--) subtracts one from its operand.
-> In other words, a=a+1 is same as ++a or a++ and b=b-1 is the same as --b or b--
-> However, both the increment and decrement operators come in two varieties :

2.1 Prefix version - In this the operator comes before the operands, as in ++a or --a.
-> For prefix increment or decrement operators follow change-then-use rule i.e., first increment or decrement the value of operand according to the operator and then use evaluate.

2.2 Postfix version - In this the operator comes after the operands, as in a++ or a--.
-> For postfix increment or decrement operators follow use-then-change rule i.e., first use the value of their operand in evaluating the expression and then change (increment or decrement) the operand's value.

EXAMPLE 1 : Evaluate x=++y + 2y if y=7.
Solution- Initially y=7, therefore ++y = 8 (according to prefix rule and after this y also becomes 8)
                and 2y=2*8=16
                hence, x=8+16=24

EXAMPLE 2 : Evaluate x=y-- + y if y=5.
Solution- Initially y=5 and according to postfix rule y-- is also equal to 5 but after this y becomes 4
                 hence, x=5+4=9

3. Relational operators- It determines the relation among different operands.
-> C++ provide six relational operators for comparing numbers and characters.
-> The six relational operators are :
   
     <   less than                         <=  less than or equal to                        ==  equal to
     >   greater than                    >=  greater than or equal to                   !=   not equal to

4. Logical operators- This refers to the ways in which these relationship among values can be connected.
-> C++ provides three logical operators to combine existing expressions. These are :

         | |     logical OR                  &&    logical AND                 !     logical NOT

4.1 logical OR- It combines two expressions which make its operands.
-> It evaluates to true if either of its operand evaluates to true.
-> (4==3) | | (5==5) this evaluates to true because second expression (5==5) is true.
-> (3<1) | | (3>5) results into false because both expressions are false.

4.2 logical AND- It also combines two expressions into one.
->  It evaluates to true when both of its operand evaluates to true.
-> (6==4) && (5==5) results into false because first expression is false.
->  (5>2) && (3>1) results into true because both expressions are true.

4.3 logical NOT- It works on single expression or operand i.e., it is a unary operator.
-> It reverses the truth value of the expression following it i.e., if the expression is true, then !expression is false or vice-versa.
-> Or if the expression is non-zero then !expression is zero (0), and if the expression is zero (0), then !expression is 1.
->  C++ considers 0 as a false value and any non-zero value as a true value. Value 0 and 1 are true and false value of the expression.
-> !(7) results into 0 (false) because 7 is non- zero (i.e., true).
-> !(0) results into 1 (true) because 0 is zero (i.e., false).
-> !(5>3) results into 0 (false) because the expression 5>3 is true i.e., 1.

5. Conditional operator- It stores a value depending upon a condition. This operator is ternary operator i.e., it requires three operands.
-> The general form of conditional operator :
                        expression1 ? expression2 : expression3 
-> If expression1 evaluates to true, then the value of the whole expression is the expression2, otherwise, the value of the whole expression is the value of expression3.

EXAMPLE : If sales=64 then evaluate :
                           bonus=sales > 50 ? 100 : 70 ;
Solution- Consider the first expression (sales > 50), if it evaluates to true then bonus=100 otherwise bonus=70. Here sales=64 which is greater than 50, therefore bonus=100.

6. Compile-time operator (sizeof)- Its a unary compile-time operator that returns the length (in bytes)
of the variable or parenthesized type-specifier that it precedes..
-> can be used in two forms :  sizeof (type)  (where type is a C++ data type)
                                                 and
                                                 sizeof var   (where var is a declared variable)
-> Consider the codes given below :
     int x;
     cout<< " The size of integer variable x is :"<<sizeof x;
     cout<< "\n The size of float is :"<<sizeof (float);

Output for the above codes will be :
 
     The size of integer variable x is :2
     The size of float is :8
















Comments

Popular Posts