*1.5K*

Operators are very important to use in C++ because, without their use, expressions cannot be evaluated.

With the help of examples, we will learn about the various types of operators in C++ in this article.

An operator is a symbol in programming that operates on a value or variable. In simple words, an operator performs operations on operands. The operation might be arithmetic or data comparison.

For example;

`x = a+b;`

In the above example, `a`

, `b`

, and `x`

are operands and `+`

and `=`

are the operators.

Operators in C++ can be classified into:

- Assignment Operators
- Arithmetic Operators
- Compound Operators
- Relational Operators
- Increment/decrement Operators
- Logical Operator

**1. Assignment Operators**

The assignment operator assigns a value to a variable. The symbol `=`

is used as an assignment operator.

For example;

`x = 26;`

The integer 26 is assigned to the variable `x`

in the above statement. The left side of the assignment operator `=`

is a variable, while the right side can be a constant value, a variable, an operation result, or a combination of these.

The assignment operation is always performed from right to left.

**2. Arithmetic Operators**

`+`

, `-`

, `*`

, `/`

, `%`

are the five arithmetic operators used in C++ that support the following arithmetic operations.

Symbol | Operation |
---|---|

+ | Addition |

- | Subtraction |

* | Multiplication |

/ | Division |

% | Modulus |

The percentage sign `%`

shown in the above table is used for modulus. The remainder of a division of two values is determined by modulus.

For example;

`Remainder = 11 % 5;`

The variable remainder will contain the value 1 since 1 is the remainder from dividing by `5`

.

**3. Compound Assignment Operators**

`+=`

, `-=`

, `*=`

, `/=`

, `%=`

are the compound assignment operators, also known as arithmetic assignment operators used in C++.

In C++, these operators are used to modify the value of a variable by executing an operation on the value currently stored in the variable.

For example;

`a+=b;`

The preceding code will be used to add the value of a variable `b`

to the value of another variable `a`

.

To make it more simple, consider the code like this:

`a=a+b;`

Operator | Symbol | Form | Operation |
---|---|---|---|

Addition Assignment | += | x += y | Addition |

Subtraction Assignment | -= | x -= y | Subtraction |

Multiplication | *= | x *= y | Multiplication |

Division Assignment | /= | x /= y | Division |

Modulus Assignment | %= | x %= y | Modulus |

Consider the following program to understand the concept of Compound Assignment Operators in C++.

// demonstrates arithmetic assignment operators #include <iostream> using namespace std; int main() { int ans = 27; ans += 10; //same as: ans = ans + 10; cout << ans; return 0; }

### OUTPUT OF THE PROGRAM

`37`

**4. Relational Operators**

Relational operators compare two expressions or, to put it another way, they check the relationship between two operands.

These operators can provide either `true`

or `false`

results.

For example;

`10 > 20;`

The above single-line statement checks if 10 is greater than 20. Here, `>`

is a relational operator. It checks whether 10 is greater than 20.

If the relationship is `true`

, it returns 1, but if it is `false`

, it returns 0.

Operator | Symbol | Form | Operation |
---|---|---|---|

Greater Than | > | x > y | If x is greater than y, true; else, false. |

Less Than | < | x < y | If x is less than y, true, else, false. |

Greater or Equal | >= | x >= y | If x is greater than or equal to y, true; else, false. |

Less or Equal | <= | x <= y | If x is less than or equal to y, true; else, false. |

Equal | ==' | x == y | if x is equals to y, true; else, false. |

No Equal | != | x != y; | if x does not equal y, true; else, false |

Consider the following program to understand the concept of Relational Operators in C++.

//A CPP Program to demonstrate the use of Relational operators #include <iostream> using namespace std; int main() { int marks; cout<<"Enter Your Marks= "<< endl; cin>> marks; if(marks!=50) cout<<"\nPass"; else cout<<"\nFail"; return 0; }

### OUTPUT OF THE PROGRAM:

```
Enter Your Marks=
43
Pass
```

**5. Increment/decrement Operators**

increment/decrement operators are also known as Unary arithmetic operators. These operators are used to shorten the length of expressions.

The increment and decrement operators `++`

and `--`

respectively increase and decrease the value stored in a variable by `1`

.

For example;

```
Var1++;
Var1 = Var + 1;
Var1 += 1;
```

The above statements are similar, and they all increase the value of the variable `Var1`

by 1.

To decrease the value of the variable `Var1`

, you just need to change the `+`

sign to `-`

.

For example;

```
Var1--;
Var1 = Var - 1;
Var1 -= 1;
```

Each increment and decrement operator has two versions—the prefix and postfix versions.

The operators precede their operands in the prefix form of the increment and decrement operators, e.g., ++Var1, –Var1.

While in postfix form, the increment and decrement operators appear in the expression after the operands, e.g., `Var1++`

, `Var1--`

.

Consider the following program to understand the concept of Increment/Decrement Operators in C++.

//increment/decrement operator example //CPP #include <iostream> using namespace std; main() { int x = 5; cout<<"Increment in prefix form ="<<++x<<endl; cout<<"Decrement In Prefix Form ="<<--x<<endl; return 0; }

**6. Logical Operators**

Logical operators are mostly used in decision-making statements. To evaluate if an expression is `true`

or `false`

, logical operators are used. If the expression is `true`

, it will return 1, but if it is `false`

, it will return 0.

There are three logical operators used in C++. These are:

- Logical NOT ( ! )
- Logical AND ( && )
- Logical OR ( || )

Operator | Symbol | Form | Operation |
---|---|---|---|

Logical NOT | ! | !x | If x is false, true; otherwise, false. |

Logical AND | && | x&&y | If both x and y are true, true; otherwise, false. |

Logical OR | || | x || y | If either x or y is true, true; otherwise, false. |

**1. Logical NOT**

The `!`

operator is the C++ unary operator that is used to perform the Boolean `NOT`

operation. The output of this operator is the inverse of the value of its operand.

X | !X |
---|---|

True | False |

False | True |

It produces `false`

if its operand is `true`

and `true`

if its operand is `false`

.

**2. Logical AND**

The logical `&&`

operator is a two-operand binary operator. It is used to check whether or not both conditions are `true`

.

X | Y | X&&Y |
---|---|---|

True | True | True |

False | False | False |

False | True | False |

The logical `&&`

returns true if both conditions are true; otherwise, it returns `false`

.

**3. Logical OR**

The logical operator `||`

is used to check if two conditions are `true`

. The logical `||`

operator returns true if either the left operand or the right operand evaluates to true.

X | Y | X || Y |
---|---|---|

True | True | True |

False | False | False |

False | True | True |

Also, if both the operands are true, logical `||`

returns to true, but if both are false, it returns to false.

Consider the following program to understand the concept of Logical Operators in C++.

// C++ program to demonstrate working of // logical operators #include <iostream> using namespace std; int main() { int a = 20, b = 40, c = 10, d = 70; // logical operators // logical AND example if (a > b && c == d) cout << "a is greater than b AND c is equal to d\n"; else cout << "AND condition not satisfied\n"; // logical OR example if (a > b || c == d) cout << "a is greater than b OR c is equal to d\n"; else cout << "Neither a is greater than b nor c is equal " " to d\n"; return 0; }

### OUTPUT OF THE PROGRAM

```
AND condition not satisfied
Neither a is greater than b nor c is equal to d
```

**Unary, Binary, and Ternary Operators**

Unary, binary, and ternary operators are the three primary categories of C++ operators.

This division is based on the number of operands that the operators operate on.

**1. Unary Operators**

Unary operators are those that only require one operand. There are two unary operators in arithmetic: unary `+`

and unary `-`

.

For example;

```
a = +x;
b = -y;
c = !y;
```

The logical NOT `!`

and the increment/decrement operators are examples of unary operators.

**2. Binary Operators**

The binary operators are those that need two operands. Example of binary operators are `+`

, `-`

, `*`

, `/`

, `%`

.

The Assignment Operator `=`

is also known as a binary operator as it also requires two operands.

In simple words, all operators that require two operands are known as binary operators.

**3. Ternary Operators**

Three operands are required for the ternary operator. Only one ternary operator exists in a statement. The operator `?:`

is a conditional operator.

Consider the following example of the Ternary operator.

//A CPP Program to find the greater number out of 3 using Ternary Operator #include <iostream> #include <conio.h> using namespace std; int main() { int num1, num2, num3, max; cout<< "Enter Three Numbers= "<< endl; cin>> num1 >> num2 >> num3 ; max = num1; max= (num2>max)?num2:max; max= (num3>max)?num3:max; cout<< "The Max Integer is ="<< max<<endl; getch(); }

### OUTPUT OF THE PROGRAM

```
Enter Three Numbers=
30
40
20
The Max Integer is =40
```