C++ Operators
Operator are used to perform mathematically and logically operation using of symbols in programming language. Those symbols are provide special meaning of compilers when applied of an operands(like variable value and constant value). That is reliable to perform mathematical task (operation) and comparison.
Binary Operators :binary operator are need at least two operands.
Unary Operators :This are applicable for single operands. normally increment and decrement operator are used in single operands.
Operator | Name |
---|---|
++ | increment operator( double plus sign) |
-- | decrement (double minus sign) |
For example
/*
Example of increment ++ and
decrement -- operator
*/
#include<iostream>
using namespace std;
int main(){
int value=10;
cout<<"Before increment value :"<<value<<endl;
value++; //increment by one
cout<<"After increment value :"<<value<<endl;
cout<<"Before decrement value :"<<value<<endl;
value--;//decrement by one
cout<<"After increment value :"<<value<<endl;
return 0;
}
Output
Before increment value :10
After increment value :11
Before decrement value :11
After increment value :10
Type of operators
There are various type of operator are supported in c++ programming. there are given as follows.
Arithmetic Operator
Arithmetic operators are used to perform mathematical operation of operands. using of these operator we can easily to calculate like addition, subtraction,multiplication, division. and also can solve mathematical equation. there are two types.
Operator | Name |
---|---|
+ | Addition( plus sign) |
- | Subtract (minus sign) |
* | multiplication (* symbol) |
/ | Divide |
% | remainder operator |
for example
//Example of Arithmetic Operator
#include<iostream>
using namespace std;
int main(){
int data1=9,data2=3;
//data1 and data2 is operands of operator
cout<<"("<<data1<<"+"<<data2<<"):"<<data1+data2<<endl;
cout<<"("<<data1<<"-"<<data2<<"):"<<data1-data2<<endl;
cout<<"("<<data1<<"*"<<data2<<"):"<<data1*data2<<endl;
cout<<"("<<data1<<"/"<<data2<<"):"<<data1/data2<<endl;
cout<<"("<<data1<<"%"<<data2<<"):"<<data1%data2<<endl;
return 0;
}
Output
(9+3):12
(9-3):6
(9*3):27
(9/3):3
(9%3):0
Logical operator
logical operator are used to comparing of single and multiple condition. this condition are based on operand. operand and logical operator are combine together and make a decision expression. this result will be true and false (0 and 1).
Operator | Name |
---|---|
&& | Logical AND operator. |
|| | Logical OR Operator |
! | Logical NOT Operator |
For example
//Example of Logic Operator (&&,||,!)
#include<iostream>
using namespace std;
int main(){
int bonus=0,amount=30000;
if(bonus || amount){
//condition 2 is true amount !=0
cout<<"bonus :"<<bonus<<" amount :"<<amount<<endl;
}
if(!bonus){
//condition is true because bonus is zero
bonus=10000;
}
if(bonus && amount){
//both condition is true (bonus !=0 and amount!=0)
cout<<"bonus :"<<bonus<<" amount :"<<amount<<endl;
}
return 0;
}
Output
bonus :0 amount :30000
bonus :10000 amount :30000
Relational operator
Relational operator are used to compare two values. this is work on two operand or two values so it also called binary operator.result of relational operator is boolean value( True and false). here given six relational operator
Operator | Name |
---|---|
> | greater than |
< | less than |
== | equal to equal to |
>= | greater than equal to |
<= | less than equal to |
!= | Not equal to |
Assignment Operators
assignment operators are used to initialize and assign new value to variables. there are denoted as (=) equal sign.
Operator | Meaning |
---|---|
= | equal |
*= | Multiply and assign |
+= | Add and assign |
/= | Divide and assign |
-= | Subtract and assign |
%= | calculate remainder and assign |
Bitwise Operators
Bitwise Operator are work in bit level. this bits are represented in from of binary numbers (0,1).perform all operation in (CPU) is work on bit-level. here given six Bitwise operators.
Operator | Meaning |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise complement |
>> | Shift Right |
<< | Shift left |
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment