C++ Operators: In this tutorial, we will find out about the various sorts of operators in C++ with the help of examples. In programming, an operator is a symbol that works on a value or a variable.

Operators are images that perform procedure on factors and qualities. For instance, + is an operator used for addition, while – is an operator used for subtraction.

Operators in C++ can be ordered into 6 sorts:

1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Other Operators

## 1. C++ Arithmetic Operators

Arithmetic operators are used to performing Arithmetic operations on variables and data. For instance,

``a + b;``

Here, the + operator is used to add two variables a and b. Similarly there are various other arithmetic operators in C++.

### Example 1: Arithmetic Operators

``````#include <iostream>
using namespace std;

int main() {
int a, b;
a = 7;
b = 2;

// printing the sum of a and b
cout << "a + b = " << (a + b) << endl;

// printing the difference of a and b
cout << "a - b = " << (a - b) << endl;

// printing the product of a and b
cout << "a * b = " << (a * b) << endl;

// printing the division of a by b
cout << "a / b = " << (a / b) << endl;

// printing the modulo of a by b
cout << "a % b = " << (a % b) << endl;

return 0;
}``````

Output

``````a + b = 9
a - b = 5
a * b = 14
a / b = 3
a % b = 1``````

Here, the operators +, – and * compute addition, subtraction, and multiplication respectively as we might have expected.

### / Division Operator

Note the operation (a / b) in our program. The / operator is the division operator.

As we can see from the above example, if an integer is divided by another integer, we will get the quotient. However, if either divisor or dividend is a floating-point number, we will get the result in decimals.

``````In C++,

7/2 is 3
7.0 / 2 is 3.5
7 / 2.0 is 3.5
7.0 / 2.0 is 3.5``````

#### % Modulo Operator

The modulo operator % computes the rest of. At the point when a = 9 is separated by b = 4, the rest of 1.

Note: The % operator must be used with integers.

## Increment and Decrement Operators

C++ likewise gives increment and decrement : operators++ and – separately. ++ builds the value of the operand by 1, while – decreases it by 1.

For instance,

``````int num = 5;

// increasing num by 1
++num;``````

Here, the value of num gets increased to 6 from its initial value of 5.

### Example 2: Increment and Decrement Operators

``````// Working of increment and decrement operators

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 100, result_a, result_b;

// incrementing a by 1 and storing the result in result_a
result_a = ++a;
cout << "result_a = " << result_a << endl;

// decrementing b by 1 and storing the result in result_b
result_b = --b;
cout << "result_b = " << result_b << endl;

return 0;
}``````

Output

``````result_a = 11
result_b = 99``````

In the above program, we used ++ and – operators as prefixes. We can likewise use these operators as postfix.

There is a slight distinction when these administrators are utilized as a prefix versus when they are used as a postfix.

## C++ Assignment Operators

In C++, assignment operators are used to assigning values to variables. For example,

``````// assign 5 to a
a = 5;``````

Here, we have assigned a value of 5 to the variable a.

### Example 2: Assignment Operators

``````#include <iostream>
using namespace std;

int main() {
int a, b, temp;

// 2 is assigned to a
a = 2;

// 7 is assigned to b
b = 7;

// value of a is assigned to temp
temp = a;    // temp will be 2
cout << "temp = " << temp << endl;

// assigning the sum of a and b to a
a += b;       // a = a +b
cout << "a = " << a << endl;

return 0;
}``````

Output

``````temp = 2
a = 9``````

## C++ Relational Operators

A relational operator is used to check the relationship between two operands. For example,

``````// checks if a is greater than b
a > b;``````

Here, > is a relational operator. It checks if a is greater than b or not.

If the relation is true, it returns 1 whereas if the relation is false, it returns 0.

### Example 4: Relational Operators

``````#include <iostream>
using namespace std;

int main() {
int a, b;
a = 3;
b = 5;
bool result;

result = (a == b);   // false
cout << "3 == 5 is " << result << endl;

result = (a != b);  // true
cout << "3 != 5 is " << result << endl;

result = a > b;   // false
cout << "3 > 5 is " << result << endl;

result = a < b;   // true
cout << "3 < 5 is " << result << endl;

result = a >= b;  // false
cout << "3 >= 5 is " << result << endl;

result = a <= b;  // true
cout << "3 <= 5 is " << result << endl;

return 0;
}``````

Output

``````3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1``````

Note: Relational operators are used in decision making and loops.

## 4. C++ Logical Operators

Logical Operators are used to checking whether an articulation is true or false. If the articulation is true, it returns 1 though if the articulation is false, it returns 0.

In C++, logical operators are generally utilized in dynamic. To additionally comprehend the logical operators, let’s see the following examples,

``````Suppose,
a = 5
b = 8

Then,

(a > 3) && (b > 5) evaluates to true
(a > 3)  && (b < 5) evaluates to false

(a > 3) || (b > 5) evaluates to true
(a > 3) || (b < 5) evaluates to true
(a < 3) || (b < 5) evaluates to false

!(a == 3) evaluates to true
!(a > 3) evaluates to false``````

### Example 5: Logical Operators

``````#include <iostream>
using namespace std;

int main() {
bool result;

result = (3 != 5) && (3 < 5);     // true
cout << "(3 != 5) && (3 < 5) is " << result << endl;

result = (3 == 5) && (3 < 5);    // false
cout << "(3 == 5) && (3 < 5) is " << result << endl;

result = (3 == 5) && (3 > 5);    // false
cout << "(3 == 5) && (3 > 5) is " << result << endl;

result = (3 != 5) || (3 < 5);    // true
cout << "(3 != 5) || (3 < 5) is " << result << endl;

result = (3 != 5) || (3 > 5);    // true
cout << "(3 != 5) || (3 > 5) is " << result << endl;

result = (3 == 5) || (3 > 5);    // false
cout << "(3 == 5) || (3 > 5) is " << result << endl;

result = !(5 == 2);    // true
cout << "!(5 == 2) is " << result << endl;

result = !(5 == 5);    // false
cout << "!(5 == 5) is " << result << endl;

return 0;
}``````

Output

``````(3 != 5) && (3 < 5) is 1
(3 == 5) && (3 < 5) is 0
(3 == 5) && (3 > 5) is 0
(3 != 5) || (3 < 5) is 1
(3 != 5) || (3 > 5) is 1
(3 == 5) || (3 < 5) is 0
!(5 == 2) is 1
!(5 == 5) is 0``````

#### Explanation of logical operator program

(3 != 5) && (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true). (3 == 5) && (3 < 5) evaluates to 0 because the operand (3 == 5) is 0 (false). (3 == 5) && (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
(3 != 5) || (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true). (3 != 5) || (3 > 5) evaluates to 1 because the operand (3 != 5) is 1 (true).
(3 == 5) || (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
!(5 == 2) evaluates to 1 because the operand (5 == 2) is 0 (false).
!(5 == 5) evaluates to 0 because the operand (5 == 5) is 1 (true).

## C++ Bitwise Operators

In C++, bitwise operators are used to perform operations on individual bits. They can only be used alongside char and int data types.

Apart from the operators discussed above, there are a few other operators, such as sizeof, ?, ., &, etc., that cannot be neatly classified into one or another type. We will learn more about these operators in later tutorials.

Please feel free to give your comment if you face any difficulty here.

C++ Introduction  