Define Operators and Its Types

Operators

Operators are the symbols that are used to perform certain operations on data. C++ provides different types of operators such as arithmetic operators, relational operators and logical operators etc. 

The operators can be categorized as follows:

Defined Operators in Programming ?

Unary Operators

A type of operator that works with one operand is known as unary operator. Some unary operators are as follows:

 - , + + , - -

The above operators are used with one operand as follows:

- - a;
N + +;
- -  x;

Binary Operators

A type of operator that works with two operands is known as binary operator Some binary operators are as follows:

+ , - , * ,  / , %

The above operators are used with two operands as follows:

  • a + b;
  • x / y;
  • a > b;
Defined Operators in Programming ?

Arithmetic Operators

Arithmetic operator is a symbol that performs mathematical operation on data. C++ provides many arithmetic operators. A list of all arithmetic operators in C++ is as follows:

Operation

Symbol

Description

Addition

+

It adds two values.

Subtraction

-

It subtracts one value from another value.

Multiplication

*

It multiplies two values.

Division

/

It divides one value by another value.

Modulus

%

It gives the remainder of division of two integers.

Some important points about modulus operator are as follows:

  • Modulus operator is also called remainder operator.
  • The modulus operator works only with integer values.
  • If modulus operator is used with the division of 0. the result will always be 0. For example the expression 0 % 5 will give 0 as a result
  • In expression like 3% 5,3 is not divisible by 5. Its result is 3.

Working of Division Operator

The manipulation of division operator is different from other operators. The result of division operation is always an integer if both the divisor and dividend are integers. The fractional part of the quotient is truncated.

Example

The result of 7.0/2.0 is 3.5 but the result of 7/2 is 3. The floating point number must be used to get the accurate result of a division operation.

Arithmetic Expression

A type of expression that consists of constants, variables and arithmetic operators is called arithmetic expression. These expressions are used to perform arithmetic operations such as addition, subtraction and division etc.

Examples

Suppose the two variables A and B have the values A=10 and B=5. 

The following table shows the arithmetic expressions and their result:

Arithmetic Expression

Result

A + B

15

A - B

5

A * B

50

A / B

2

A % B

0


Assignment Operator

The assignment operator is used to assign a value to a variable. The symbol = is used as assignment operator in C++.A statement that assigns a value to a variable is known as assignment statement.

The name of variable is written on the left side of the assignment operator and the value is written on the right side of assignment operator. The value can be a constant, variable or expression or function.

Syntax

The syntax of writing an assignment statement is as follows

var = expression;

var

It is the name of variable to which the value is assigned.

=

It is the assignment operator used to assign the value to variable.

expression

It is the expression whose returned value is assigned to the variable. It can be a constant, variable or a combination of operands and arithmetical operators The expression on the right side is evaluated first. The result is then assigned to the variable on the left side.

Examples

Some examples of assignment statements are as follows:

  • A=100;
  • C=A+B;
  • X=C-D+10;

Lvalue and Rvalue

The Ivalue is an operand that can be written on the left side of assignment operator = It must always be a single value. The rvalue is an operand that can be written on the right side of assignment operator =. A Ivalues can be used as rvalues but all rvalues cannot be used as Ivalues. For example, a constant can be used as rvalue but it cannot be used as Ivalue The expression x=5 is valid but the expression 5=x is not valid.

C++ Program that performs all mathematical operations on two variables.

#include <iostream.h>

#include <conio.h>

void main()

{

clrscr();

int a, b;

a=10;

b=5;

cout <<”a+b=”<< a + b<<endl;

cout <<”a-b=”<< a + b<<endl;

cout <<”a*b=”<< a + b<<endl;

cout <<”a/b=”<< a + b<<endl;

cout <<”a%b=”<< a + b<<endl;

getch();

}


Compound Assignment Statement

An assignment statement that assigns a value to many variables is known as compound assignment statement. The assignment operator is used in this statement.

Example

  • A=B=10;

The above statement assigns the value 10 to both A and B Some examples of compound assignment statement are as follows

  • x=y=z=100;
  • m=n=50;

Arithmetic Assignment Operators

C++ provides arithmetic assignment operators that combine assignment operator with arithmetic operators. These operators are used to perform arithmetic operations more easily. It is also called compound assignment operator.

Syntax

var=op=expression;

The general syntax of compound assignment operator is as follows:

Var

It is the variable to assign a value.

Op

It can be any arithmetic operator.

expression

It can be a constant, variable or arithmetic expression

Examples

Some examples of arithmetic assignment operator are as follows:

Operator

Example

Equivalent to

+=

A+=10

A=A+10

-=

A-=10

A=A-10

*=

A*=10

A=A*10

/=

A/=10

A=A/10

%=

A%=10

A=A%10

C++ program that performs all compound assignment operations on an integer.

#include <iostream.h>

#include <conio.h>

 void main()

{

int a;

clrscr();

a = 10;

cout<<"Value of a: "<<a<<endl;

 a+=5;

cout<<"Value of a after a+=5 : "<<a<<endl;

a-=5;

cout<<"Value of a after a-=5 : "<<a<<endl;

a^=2;

cout<<"Value of a after a^=2 : "<<a<<endl;

a /=2;

cout<<"Value of a after a/=2 :"<<a<<endl;

a%=2;

cout<<"Value of a after a%=2: "<<a<<endl;

getch();

}


Increment Operator

The increment operator is used to increase the value of a variable by 1. It is denoted by the symbol ++. It is a unary operator and works with single variable. It cannot increment the value of constants and expressions. For example, A++ and X++ are valid statements but 10++ is an invalid statement. Similarly, (a+b)++ or ++(a+b) are also invalid.

The increment operator can be used in two forms:

  • Postfix Form
  • Prefix Form

Prefix Form

In prefix form, the increment operator is written before the variable as follows:

++y;

The above line increments the value of variable y by 1.

Postfix Form

In postfix form, the increment operator is written after the variable as follows: 

y++;

The above line also increments the value of variable y by 1.

Defined Operators in Programming ?

Difference between Prefix & Postfix Increment

The prefix and postfix forms work similarly. when the increment operator is used independently. For example, the result of A++ and ++A is same. The prefix and postfix forms work differently when the increment operator is used in a larger expression with other operators. For example, the results of two statements A=++B and A=B++ are different.

The statement A=++B contains two operators ++ and = It works in following order

  • It increments the value of B by 1
  • It assigns the value of B to A

The above statement is equivalent to the following two statements:

++B;

A=B;

In postfix form, the statement A=R++ works in the following order:

  • It assigns the value of B to A
  • It increments the value of B by 1

The above statement is equivalent to the following two statements:

A=B;

B++; 

C++ Program that explains the difference of postfix increment operator and prefix increment operator used as independent expression.

#include <iostream.h>

#include <conio.h>

void main()

{

clrscr();

int a, b, x, y;

a=b=x=y=0 ;

a++;

b=a;

++x;

y=x;

cout <<”a= "<<a<<endl;

cout<<" b = "<<b<<endl;

cout<<"x = "<<x<<endl;

cout<<" y = "<<y<<endl;

getch();

}

C++ Program that explains the difference of postfix increment operator and prefix increment operator used as part of a larger expression.

#include <iostream.h>

#include <conio.h>

void main()

{

int a, b, x, y;

clrscr();

a = b = x = y = 0;

b = a++;

y = ++x;

cout<<"a= "<<a<<endl<<" b = "<<b<<endl;

cout<<"x = "<<x<<endl<<" y = "<<y<<endl;

getch();

}

Decrement Operator

The decrement operator is used to decrement the value of a variable by 1. It is denoted by the symbol. It is a unary operator and works with single variable. It cannot decrement the value of constants and expressions. For example, A--and X-- are valid statements but 10- is an invalid statement. Similarly, (a+b)--or--(a+b) are also invalid.

The decrement operator can be used in two forms:

  • Prefix Form
  • Postfix Form

Prefix Form

In prefix form, decrement operator is written before the variable as follows:

--у;

The above line decrements the value of variable y by 1.

Postfix Form

In postfix form, the decrement operator is written after the variable as follows:

y--;

The above line also decrements the value of variable y by 1.

Difference between Prefix & Postfix Decrement

The prefix and postfix forms work similarly when the decrement operator is used independently. For example, the result of A--and--A is same The prefix and postfix forms work differently when the increment operator is used in a larger expression with other operators. For example, the results of two statements A=--B A=B-- are different.

The statement A=--B contains two operators Le and It works in the following order:

  • It decrements the value of B by 1.
  • It assigns the value of B to A.

The above statement is equivalent to the following two statements:

--B;

A=B;

In postfix form, the statement A=B- works in the following order:

  • It assigns the value of B to A.
  • It decrements the value of B by 1.

The above statement is equivalent to the following two statements:

A=B;

B--;

Write a program that explains the difference of postfix decrement operator and prefix decrement operator used as independent expression.

#include <iostream.h>

#include <conio.h>

void main()

{

clrscr();

int a, b, x, y;

a=b=x=y=0 ;

a++;

b=a;

++x;

y=x;

cout <<”a= "<<a<<endl;

cout<<" b = "<<b<<endl;

cout<<"x = "<<x<<endl;

cout<<" y = "<<y<<endl;

getch();

}

C++ Program that explains the difference of postfix increment operator and prefix increment operator used as part of a larger expression.

#include <iostream.h>

#include <conio.h>

void main()

{

int a, b, x, y;

clrscr();

a = b = x = y = 0;

b = a++;

y = ++x;

cout<<"a= "<<a<<endl<<" b = "<<b<<endl;

cout<<"x = "<<x<<endl<<" y = "<<y<<endl;

getch();

}


Operator Precedence

The order in which different types of operators in an expression are evaluated is known as operator precedence. It is also known as hierarchy of operators. Each operator has its own precedence level. The operators with higher precedence are evaluated before the operators with lower precedence if an expression contains different types of operators.


The following table shows the order of precedence in C++. The operators with the highest precedence are written at the top of the table. The operators with the lowest precedence are written at the bottom of the table. Any expression written in parenthesis is always evaluated first.

Precedence

Operator

Operater Name

1

!

Logical Not

2

*,/,%

Multiplication, division

3

+,-

Addition, subtraction

4

<,<=,>,>=

Relational operator

5

== , !=

Relational operator

6

&&

Logical AND

7

||

Logical OR

8

+=,-=,*=,/=,%=

Compound assignment

9

=

Assignment Operator

Operator Associativity

The order in which operators of same precedence are evaluated is known as operator associativity. If an expression contains some operators that have same precedence level, the expression is evaluated either from left-to-right or right-to-left.

Post a Comment

0 Comments