OPERATORS IN C++

Seema Roselin

Operators In C++:

Operators are symbols that perform operations on values and variables.

Operators in C++ can be classified into :

  • Arithmetic Operator
  • Relational Operator
  • Logical Operator
  • Bitwise Operator
  • Assignment Operator

Arithmetic Operator:

This operator is used to perform arithmetic or mathematical operations on the operands.

+

Addition

-

Subtraction

*

Multiplication

Division

%

Modulus

++

Increment

--

Decrement

Program :

#include <iostream>
using namespace std;
int main() 
{
  int a = 40 + 60;
  cout << a;
  return 0;
}

Output :

100

Relational operator:

Relational operator is used to compare the operand. It is also known as comparison operator.The return value of a comparison is either 1 or 0, that is true (1) or false (0). These values are known as Boolean values.


  ==

Equal to

a == b 

  !=

Not equal 

a != b

Greater than 

a > b


  < 

Less than 

a < b


>=

Greater than or equal to

a >= b


<=  

Less than or equal to

a<= b


Program:

#include <iostream>
using namespace std;
int main() {
  int a = 5;
  int b = 3;
  cout << (a > b);
  return 0;
}

Output:

1

Logical operator :

Logical operator is used to determine the logic between operands.


&&       

Logical and 

It returns true if both statements are true

 a < 3 && a < 5


||

Logical or  

It returns true if one of the statements is true

 a < 2 || a < 3

!       

Logical not

It reverse the result and returns false if the result is true

!(a < 5 && x < 8)


Bitwise operator:

  • Bitwise operator is used on (binary) numbers.
  • It perform operations on integer data at the individual bit.


&

AND

  a & b

OR 

a | b

XOR

a ^ b

<< 

left shift 

a<< 4

>> 

right shift

a>>4

Assignment operator:

Assignment operator is used to assign values to variables.

=       

  a = 6  

+=       

a += 4  

-=   

a -= 4

*=  

a *= 4   

/=    

a /= 4 

%=  

a %= 4 

&=         

a &= 4

|=      

a |= 4 

^=      

a ^= 4 

>>=    

a >>= 4  

<<=     

a <<= 4

Program:

#include <iostream>

using namespace std;


int main() {

  int a = 20;

  a += 15;

  cout << a;

  return 0;

}


Output:

35




Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send