Quick Links
Operators : An operator is a symbol which represents a particular operation like(+,-,*,/ etc.) that can be performed on operands. An operand(variables) is the object on which an operation is performed.
Types of operator in C++
The operators are further classified into 6 parts:
i)Arithmetic operator.
ii)Relational operator.
iii)Logical operator.
iv) Assignment operator.
v)Increment/Decrement operator.
vi)Conditional operator
Arithmetic Operator :
In this operator all the basic operators comes:
like -: +, -, *, / and %
Example :
#include<iostream.h>
void main()
{
int a=2,b=6;
cout<<addition<<a+b;
cout<<subtraction<<b-a;
cout<<multiplication<<b*a;
cout<<division<<b/a;
cout<<modulus<<b%a;
}
Output :
addition 8
subtraction 4
multiplication 12
division 3
modulus 0
Relational Operator :
Relational operator are used to make comparision between two entities or two variable.
Operators : == , >,< ,<=,>=,!=
Example :
#include<iostream.h>
void main()
{
int a=2,b=6;
cout<<”a==b”<<a==b;
cout<<”a>b”<<a>b;
cout<<”a<b”<<a<b;
cout<<”a<=b”<<a<=b;
cout<<”a>=b”<<a>=b;
cout<<”a!=b”<<a!=b;
}
Output : a== b : false
a>b : false
a<b : True
a<=b : True
a>=b: False
a!=b : True
Logical Operators :
A piece of data is called logical if it conveys the idea of true or false. In C++ we use int data type to represent logical data. If the data value is zero, it is considered as false. If it is non -zero (1 or any integer other than 0) it is considered as true. There are three logical operator in c++:
I ) AND (&&)
II) OR(||)
III) NOT(!)
Example :
# include <iostream.h>
void main()
{
a=0;b=0;
cout<<” a&&b “<< a&&b<<endl;
cout<<” a||b “<< a||b<<endl;
cout<<” a||b “<< a||b<<endl;
}
Output:
0&&0=0
0||0=0
!0 =1
Assignment Operators :
As from the word assignment we can relate that it means assign. It is used to assign the value to the variable. It is denoted by equal sign(=).
Example:
# include<iostream.h>
void main()
{
a =3; //Assigning the value to a variable
b= 2;
cout<<a<<endl;
cout<<b<<endl;
}
Output :
3
2
Increment/Decrement operators :
The operator ++ adds one to its operand where as the operator – – subtracts one from its operand.
Example :
#include<iostream.h>
void main()
{
a=2;
b=3;
++a;
cout<<“a=”<<a<<endl;
–b;
cout<<“b=”<<b<<endl;
}
Output:
a=3
b= 2
CONDITIONAL OPERATOR:
Conditional operator requires two operands to operate or to perform task it is also known as ternary operator.
Example :
#include
void main()
{
int a, b,c;
cout<<“Enter a and b values:”;
cin>>a>>b;
c=a>b?a:b;
cout<<“largest of a and b is “<<c;
}
Output :
Enter a and b values:2 6
largest of a and b is 6
Pingback: Control Statement In C++ - AskAtul.com