# Operators

0 kelseylittleton November 13, 2020

An Operator is a symbol that operates on a certain data type or data item. Operators are used in programs to perform certain mathematical or logical manipulations. For example, in a simple expression 10+12, the symbol + is called an operator that operates on two data items 10 and 12. The data items that operators act upon are called operands.

## Types of Operator:

### 1. Arithmetic Operator

C++ provides all the basic arithmetic operators. Arithmetic operators perform arithmetic operators. There are five arithmetic operators in C++.

Note: Operands acted upon by arithmetic operators must represent numeric values and second operands of division must be non zero.

Let’s look at one example to clear the concept of an Arithmetic operator.

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

int main(){

int a = 10, b = 5;

// All Arithmetic Operators
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << a * b << endl;
cout << "a / b = " << a / b << endl;
cout << "a % b = " << a % b << endl;

return 0;
}``````

The output of the above program is:

``````a + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0``````

### 2. Relational Operator

Relational operators are used to comparing two similar operands, and depending on their relation, take some actions. Relational operators compare their left-hand side operand with their right side operand dor lesser than, greater than, less than or equal to, greater than or equal to, equal to or not equal to relations. The value of a relational expression is either 1 (if the condition is true) or 0 (if the condition is false). C++ supports six relational operators.

These relational operators are used in decision-making statements. (if, if…..else, if….else if ….else if…..else, nested if….else, and while) which will learn in the next chapter.

Let’s look at an example to make clear concepts on Relational Operators.

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

int main(){

int a = 20, b = 6;

// Relational Operators, Here we have used if condition. do not focus on that, we will learn that in next chapter
if(a > b){
cout << "a > b = TRUE" << endl;
}else{
cout << "a > b = FALSE" << endl;
}

return 0;
}``````

The output of the above program is:

``a > b = TRUE``

### 3. Logical Operator

Logical operators are using to compare or evaluate logical and relational expressions. The operands of these operators must produce either 1 (true) or 0 (false). The whole result produces by these operators is also either trueor false. There are three logical operators in C++.

• The logical AND (&&) produces trueif each expression or operand is true, otherwise, it produces false.
• The logical operator OR (||) produces trueif one of the expressions is true, otherwise, it produces false.
• The logical NOT (!) alters the operands.

This can be explained by the following table.

Let’s look at an example to make clear concepts on Logical Operator.

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

int main(){

int a = 10, b = 5;
float c = 12.5, d = 10.2;

// Logical Operators, Here we have used if condition. do not focus on that, we will learn that in next chapter
if(a > b && c <= d){
cout << "a > b and c <= d= TRUE" << endl;
}else{
cout << "a > b and c <= d = FALSE" << endl;
}

return 0;
}``````

The output of the above program is:

``a > b and c <= d = FALSE``

### 4. Assignment Operator

Assignment operators are used to assigning the result of an expression to a variable. The usual assignment operator is “=”.  The general form of shorthand assignment operators for the operation,

``````exp1 = exp1 opr exp2
is,
exp1 opr = exp2``````

For example,

• a += b is equivalent to a = a + b
• a -= b is equivalent to a = a – b
• a *= b is equivalent to a = a * b
• a /= b is equivalent to a = a / b
• a %= b is equivalent to a = a % b

### 5. Bitwise Operator

Bitwise operators are used for manipulating data at the bit level. These operators are used for testing the bits or shifting them to the left or to the right. The bitwise operator can be applied only to inter-type operands and not to float or double.