Operators in C++.

Introduction

C++ is very rich in built-in operators. In fact, C++ places more significance on operators than most other computer languages do. C++ offers different classes of operators : arithmetic, relational, logical, increment-decrement, conditional, bitwise etc. 

OPERATORS

The operations being carried out on data, are represented by operators. C++,s rich set of operators comprises of arithmetic, relational, logical and certain other type of operators. 

Let us discuss these operators in detail.

1. Arithmetic Operators

To do arithmetic, C++ uses operators. It provides operators for five basic arithmetic calculations: addition, subtraction, multiplication, division and remainder which are +, -, *, /, and %  respectively. Each of these operators is a binary operator i.e., it requires two values (operands) to calculate a final answer. Apart from these binary operators, C++ provides two unary arithmetic operators (that require one perand) also which are unary +, and unary -.

1.1 Unary Operators

 i. Unary +

The operator unary + precedes an operand. The operand (the value on which the operator operates ) of the unary + operator must have arithmetic or pointer type and the result is the value of the argument. For example,


  • if a = 5 then +a means 5.
  • if a = 0 then +a means 0.
  • if a= -4 then +a means -4.

 ii. Unary -

The operator unary - precedes an operand. The operand of the unary - operator must have arithmetic type and the result is the negation of its oprand's value. For exam;le,

  • if a = 5 then -a means -5.
  • if a = 0 then -a means 0.
  • if a= -4 then -a means +4.


1.2 Binary Operators


The operands of a binary operator are distinguished as the left or right operand. Together, the operator and its operands constitute an expression.

i. Addition operator +

The arithmetic binary operator + adds values of its operands and the result is the sum of the values of  its two operands. For example,
  • 4+20 results in 24.
  • a+5(a=2) results in 7

Its operands may be of integer or float types.

ii. Subtraction Operator -

The - operator subtracts the second operand from the first, For example,
  • 11-3 evaluates to 8
  • a-b (a=7, b=2) evaluates to 5

The operands may be of integer or float types.

iii. Multiplication Operator *

The * operator multiplies the values of its operands. For example,
  • 3*5 evaluates 15
  • a*6 (a=2) evaluates 12

The operands may be of integer or float types.

iv. Division Operator /

The / operator divides its first operand by the second. For example,
  • 100/10 evaluates 10
  • a/2 (a=6) evaluates 3.

The operands may be of integer, float or double types.


v. Modulus Operator %

The % operator finds the modulus of its first operand relative to the second. That is, it produces the remainder of dividing the first by the second operand. For example,

  • 20 % 3 evaluates to 2, since 3 goes int 20 six times with a remainder 2.

Both operands must be integer types here.

2. Relational Operators

In the term relational operator, relational refers to the relationships that values (or operands) can have with one another. Thus, the relational operators determine the relation among different operands. C++ provides six relational operators for comparing numbers and characters. But they don't work with strings. If the comparison is true, the relational expression results into the value 1 and to 0, if the comparison is false.

OperatorDescriptionExample
==Checks if the values of two operands are equal or not, if yes then condition becomes true.(A == B) is not true.
!=Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.(A != B) is true.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(A > B) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(A < B) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(A >= B) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(A <= B) is true.

3. Logical Operators 

The previous section discussed about relational operators that establish relationships among the values. This section talks about logical operators that refer to the ways these relationships (among values ) can be connected. C++ provides three logical operators to combine existing expressions. These are || (logical OR), && (logical AND )
and !(logical NOT). Let us examine them now

OperatorDescriptionExample
&&Called Logical AND operator. If both the operands are non-zero, then condition becomes true.(A && B) is false.
||Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true.(A || B) is true.
!Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false.!(A && B) is true.





Post a Comment

0 Comments