# Operators in C++

## Introduction to C++ Operators

C++ has various types of operators like** arithmetic operators, assignment operators, logical operators, relational operators, and bitwise operators.** Operators are symbols that inform the compiler to perform the mathematical operations. Arithmetic Operators are used to performing mathematical operations like addition, subtraction, multiplication, and division. Assignment Operators are used to assigning values to another variable. Logical Operators identify the logic between variables.

## What are operators in C++

The types of operators used in C++ are the following.**Arithmetic Operators****Relational Operators****Logical Operators****Assignment Operators**

**1. Arithmetic Operators:**

These operators are used to perform arithmetic operations as described below
**a) Operator: +**

Description: addition of two operands

Example: 3+5=8

**b) Operator: –**

Description: subtraction of right operand from the left operand

Example: 5-3=2

**c) Operator: ***

Description: multiplication of two operands

Example: 5*3=15

**d) Operator: /**

Example: 10/5=2

**e) Operator: %**

Description: the modulus-the remainder of the left operand by the right operand

Example: 8/4 will give 0

**f) Operator: ++**

Description: the increment operator which increases the value of operand by 1

Example: 9++ will give 10

**g) Operator: —**

Description: the decrement operator which decreases the value of operand by 1

Example: 3– will give 2

**Deprecated**: Function _register_controls is

**deprecated**since version 3.1.0! Use Elementor\Controls_Stack::register_controls() instead. in

**/home/u246705441/domains/gangofcrypto.com/public_html/wp-includes/functions.php**on line

**5379**

```
//Program in C++ using Arithmetic Operators.
#include<iostream>
using namespace std;
int main(){
//intialization
int a=1, b=2,;
//+ Operator
c = a+b;
cout<<"\n Addition is "<<c;
//- Operator
c = a-b;
cout<<"\n Subtraction is "<<c;
//* Operator
c = a*b;
cout<<"\n Product is"<<c;
// / Operator
c = a/b;
cout<<"\n Division is "<<c;
// % Operator
c = a%b;
cout<<"\n Remainder is "<<c;
// ++ Operator
a = a++;
cout<<"\n Incremented value of a is"<<a;
//-- Operator
a = a--;
cout<<"\n Decremented Value of a is "<<a;
}
```

**OUTPUT:**

Addition is 3

Subtraction is -1

Product is2

Division is 0

Remainder is 1

Incremented value of a is 1

Decremented Value of a is 1

### 2. Relational Operators

These operators are used to compare the values between operand and return TRUE or FALSE according to the condition specified in the statement.**a) Operator: >**

Description: if the value of the left operand is greater than the right operand, then the condition becomes TRUE; if not FALSE.

Example: x>y

**b) Operator: <**

Description: if the value of the left operand is less than the value of the right operand, then the condition becomes TRUE; if not FALSE.

Example: x<y

**c) Operator: ==**

Description: if both operands have equal value then the condition becomes TRUE; if not FALSE.

Example: x==y

**d) Operator: !=**

Description: if both operands do not have equal value then the condition becomes TRUE; if not FALSE.

Example: x != y

**e) Operator: >=**

Description: if the value of the left operand is greater than equal to the right operand then the condition becomes TRUE; if not FALSE.

Example: x>=y

**f) Operator: <=**

Description: if the value of the left operand is less than or equal to the right operand then the condition becomes TRUE; if not FALSE.

Example: x<=y

Let us assume the value of operands a=8, b=4, and perform different operations to understand the relational operators.

- a>b will give the result TRUE as 8 is greater than 4.
- a
- a==b will give the result FALSE as 8 is not equal to 4.
- a != b will give the result TRUE as 8 is not equal to 4.
- a >= b will give the result TRUE as 8 is greater than 4.
- a<= b will give the result FALSE as 8 is not equal to or less than 4.

```
#include<iostream>
using namespace std;
int main(){
//Initialization
int a=8, b=4;
// > Operator
if(a>b){
cout<<"\n TRUE";
}
else{
cout<<"\n False";
}
// < Operator
if(a<b){
cout<<"\n TRUE";
}
else{
cout<<"\n False";
}
// == Operator
if(a==b){
cout<<"\n TRUE";
}
else{
cout<<"\n False";
}
// != Operator
if(a!=b){
cout<<"\n TRUE";
}
else{
cout<<"\n False";
}
// >= Operator
if(a>=b){
cout<<"\n TRUE";
}
else{
cout<<"\n False";
}
// <= Operator
if(a<=b){
cout<<"\n TRUE";
}
else{
cout<<"\n False";
}
}
```

**OUTPUT:**

TRUE

False

False

TRUE

TRUE

False

### 3. Logical Operators

These operators are used in C++ as shown below.**a) Operator: ||**

Description: It is the logical OR Operator. The condition becomes TRUE if any of the two operands are non-zero.

Example: x||y

**b) Operator: &&**

Description: It is the logical AND Operator. The condition becomes TRUE if both of the two operands are non-zero.

Example: x&&y

**c) Operator: !**

Description: It is the logical NOT Operator. It reverses the state of the logical operator with which it is used.

Example: !x

Let us assume the value of operands x=0, y=1, and perform different operations to understand the logical operators.

- x||y will be TRUE as one of the two operands is non-zero.
- x&&y will be FALSE as one of the two operands is zero.
- !x will be 1 as it reverses the state of the operand.

```
//program using Logical Operators in CPP
#include<iostream>
using namespace std;
int main(){
//Initilization
int x=0, y=1;
//OR Operator
cout<<"\n x||y is "<<(x||y);
//AND Operator
cout<<"\n x&&y is "<<(x&&y);
//NOT Operator
cout<<"\n !x is"<<(!x);
}
```

**OUTPUT:**

x||y is 1

x&&y is 0

!x is1

### 4. Assignment Operators

These operators in C++ are used in C++ as shown below.**a) Operator: =**

Description: This is a simple assignment operator that assigns the value of the right side operand to the left side operand.

Example: x=y will assign the value of y to x.

**b) Operator: +=**

Description: This operator performs the addition of the right operand to the left operand and the result is assigned to the left operand.

Example: x += y is equal to x = x+y

**c) Operator: -=**

Description: This operation performs subtraction of right operand from the left operand and the result is assigned to the left operand.

Example: x -= y is equal to x= x-y

**d) Operator: *=**

Description: This operator performs multiplication of the right operand with the left operand and the result is assigned to the left operand.

Example: x *= y is equal to x = x*y

**e) Operator: /=**

Description: This operator performs division of left operand with the right operand and the result is assigned to the left operand.

Example: x /= y is equal to x = x/y

**f) Operator: %=**

Description: This takes the modulus of two operands and the result is assigned to the left operand.

Example: x %= y is equal to x = x % y

**g) Operator: >>=**

Description: This ia binary right shift and assignment operator.

Example: x >>= 4 is equal to x = x>>4

**h) Operator: <<=**

Description: This is a binary left shift and assignment operator.

Example: x <<= 4 is equal to x = x<<4

**i) Operator: ^=**

Description: This is called bitwise exclusive OR and assignment operator.

Example: x ^= 4 is equal to x = x^4

**j) Operator: &=**

Description: This is called bitwise exclusive AND assignment operator.

Example: x &= 4 is equal to x = x & 4

Let us assume the value of x as 4 and perform different operations to understand the assignment operators.

- x = 4 will assign the value of 4 to x.
- x += 3 will give thke result as x = x + 3 i.e. 4+3 = 7 will be assigned to x.
- x-= 2 will give the result as x = x-2 i.e. 4-2 = 2 will be assigned to x.

```
//Program using Assignment Operators in cpp
#include<iostream>
using namespace std;
int main(){
//Initialization
int x,y;
//= Operator
x=4,y=5;
//+= Operator
x+=y;
cout<<"\n Result of += is "<<x;
//-= Operator
x=4,y=5;
x-=y;
cout<<"\n Result of -= is "<<x;
//*= Operator
x*=y;
cout<<"\n Result of *= is "<<x;
// /= Operator
x/=y;
cout<<"\n Result of /= is"<<x;
// %= Operator
x%=y;
cout<<"\n Result of %= is"<<x;
// ^ Operator
cout<<"\n Result of ^ is"<< (x^y);
// & Operator
cout<<"\n Result of & is"<< (x&y);
}
```

**OUTPUT:**

Result of += is 9

Result of -= is -1

Result of *= is -5

Result of /= is-1

Result of %= is-1

Result of ^ is-6

Result of & is5