#### PREV NEXT

**What are operators?**

The operators are the special type of functions that takes one or more parameters and gives new result. It is a symbol that tells the compiler to perform the mathematical and logical manipulations. The programming language like C or C++ is incomplete without the use of operators.

# Some of the built in operators are

- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Bitwise operators

So, let us have a look all these operators briefly.

# Arithmetic Operators

The arithmetic operators are used to perform the arithmetic operations on the operands. The operations can be addition, multiplication, subtraction and division.

** Let us have a look at the example:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include<stdio.h> int main() { int x = 5, y = 10, z; //printing x and y printf("x is %d and y is %d\n", x, y); z = x+y; //addition printf("x+y is %d\n", z); z = x-y; //subtraction printf("x-y is %d\n", z); z = x*y; //multiplication printf("x*y is %d\n", z); z = x/y; //division printf("x/y is %d\n", z); z = x%y; //modulus printf("x%y is %d\n", z); z = x++; printf("x++ is %d and z is %d\n",x, z); z = x--; printf("x-- is %d and z is %d\n",x, z); return 0; } |

# COMPILE & RUN

# Relational Operators

The relational operators are those operators that are used to compare the values of two operands. For example, by comparing two operands that their values are equal or not, or the value of one operand is greater than the other.

# Let us have a look at the example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include<iostream> using namespace std; int main() { int x = 5, y = 10, z; //printing x and y cout<<"\nArithmetic Operators" ; z = x+y; //addition cout<<"\nx+y is ="<<z; z = x-y; //subtraction cout<<"\nx-y is ="<<z; z = x*y; //multiplication cout<<"\nx*y is ="<<z; z = x/y; //division cout<<"\nx/y is = "<<z; z = x%y; //modulus cout<<"\nx%y is ="<<z; z = x++; cout<<"\nx++ is and z is ="<<x<<z; z = x--; cout<<"\nx-- is and z is ="<<x<<z; return 0; } |

# COMPILE & RUN

# Logical Operators

The logical operators are those operators that are used to combine two or more conditions. The logical operators are AND (&&) and OR (||). If we combine two statements using AND operator then only both the valid statements will be considered and if we combine two statements using OR operator then only either one of them will be considered.

# Let us have a look at the example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> using namespace std; int main() { int x = 20, y = 5, z = 10, a = 15; // logical AND if (x>y && z==a) cout<<"\nx is greater than y AND z is equal to a"; else cout<<"\nAND condition not satisfied"; // logical OR if (x>y || z==a) cout<<"\nx is greater than y OR z is equal to a"; else cout<<"\nNeither x is greater than y nor z is equal to a"; // logical NOT if (!x) cout<<"\nx is zero"; else cout<<"\nx is not zero"; return 0; } |

# COMPILE & RUN

# Assignment Operators

The assignment operators are those operators which are used to assign value to a variable. On the left side of the assignment operator the operand is a variable and the right side of the operator the operand is a value. The value on the right side of the operator should be of the same data type as of variable on the left side of the operator otherwise it will show a compile error.

# Let us have a look at the example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include<iostream> using namespace std; int main() { int x = 15, y = 5; //printing x and y cout<<"\nx is = "<<x<<" and y is = "<< y; x += y; //addition cout<<"\nx+y is="<< x; x -= y; //subtraction cout<<"\nx-y is = "<<x; x *= y; //multiplication cout<<"\nx*y is ="<<x; x /= y; //division cout<<"\nx/y is = "<< x; x %= y; //modulus cout<<"\nx%y is = "<< x; return 0; } |

# COMPILE & RUN

# Bitwise Operators

The bitwise operators are those are used to perform bit level operations on the operands. In the bitwise operators first operators are converted to bit level and then calculation is performed on the operands. Some of the operations which are performed are addition, subtraction, multiplication, division, etc.

# Let us have a look at the example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <iostream> using namespace std; main() { unsigned int x = 10; // 10 = 00001010 unsigned int y = 5; // 5=00000101 int z = 0; z = x & y; // 00000000 cout << "Value of z is : " << z << endl ; z = x | y; // 00001111 cout << "Value of z is: " << z << endl ; z = x ^ y; // 00001111 cout << "Value of z is: " << z << endl ; z = ~x; // 11110101 cout << "Value of z is: " << z << endl ; z = x << 2; // 00101000 cout << "Value of z is: " << z << endl ; z = x >> 2; // 00000010 cout << "Value of z is: " << z << endl ; return 0; } |

# COMPILE & RUN

# What are expressions?

An expression is a sequence of operators and the operands. It is a form when you combine operands and operators.

**The expressions are of three types**

- Arithmetic expression
- Relational expression
- Logical expression

# Arithmetic Expression

An arithmetic expression is that expression in which arithmetic operators are used. Like addition, multiplication, subtraction, division, etc.

# Relational Expression

A relational expression is that expression in which relational operators are used. The operators provided in relational expression are less than (<), greater than (>), less than equal to (<=), greater than equal to (>=), etc.

# Logical Expression

A logical expression is that expression in which logical operators are used. Some of the logical operators are AND (&&), OR (||), NOT (!).