Hi guys, we will discuss Operators and their types in this section. This section is very important to clear your concepts about operators and their uses. So without wasting time let's get started.
Operators:
- Operators are the symbols that are used to perform some mathematical and logical operations.
- Basically, operators are the instructions to prepare a proper executable program code.
Types Of Operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Conditional Operators
- Sizeof ( ) Operator[also called sizeof () function]
- Increment & Decrement Operators
1. Arithmetic Operators:
- Arithmetic Operators are used to perform common mathematical operations.
- The arithmetic operators are: [ +, -, *, / and %]
- Let's follow the table for a better understanding of arithmetic operators:
2. Relational Operators:
- The Relational Operators are also known as Comparision Operators.
- These operators are used to compare two operands to one another.
- The relational operators are: [==, !=, <, >, <= and >=]
Equal to (==):
* This equal to / equals to equals operator is used to check if the values of two operands are equal or not. If yes then the condition becomes true.
* Example:
x = 2
y = 2
(x == y) o/p: TRUE
x = 2
y = 3
(x == y) o/p: FALSE
Not equal to (!=):
* This operator is used to check if the values of two operands are equal or not.
* If the values are not equal, then the condition becomes true.
* Example:
x = 2
y = 3
(x != y) o/p: TRUE
x = 3
y = 3
(x != y) o/p: FALSE
Greater than (>):
* This greater-than operator is used to check if the value of the left operand is greater than the right operand. If yes, then the condition becomes true.
* Example:
x = 2
y = 3
(x > y) o/p: FALSE
x = 5
y = 2
(x > y) o/p: TRUE
Less than (<):
* This less-than operator is used to check if the value of the left operand is less than the value of the right operand. If yes, then the condition becomes true.
* Example:
x = 2
y = 3
(x < y) o/p: TRUE
x = 5
y = 2
(x < y) o/p: FALSE
Greater than equal to (>=):
* This operator is used to check if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true.
* Example:
x = 6
y = 3
(x >= y) o/p: TRUE
x = 4
y = 5
(x >= y) o/p: FALSE
Less than equal to (<=):
* This operator is used to check if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true.
* Example:
x = 2
y = 3
(x <= y) o/p: TRUE
x = 3
y = 3
(x <= y) o/p: TRUE
x = 9
y = 3
(x <= y) o/p: FALSE
3. Logical Operators:
- Logical operators are also known as Boolean Operators.
- These operators are used to make a decision on two conditions.
- Logical operators are typically used with boolean values and they also return boolean values.
- These Logical Operators are used along with the relational operators to make a proper meaning and sense.
- The logical operators are used to perform logical operations i.e. true or false values.
- These Operators are ( &&, ||, !) which are discuss in below table with examples:
Note:
AND : True && True = True OR : True && False = True
NOT : Used To Reverse Results
[True-False and False-True]
4. Bitwise Operators:
- A Bitwise operator works on bits and performs (bit-by-bit) operations.
- These operators manipulate the values of data at the bit level.
- Example:
In Binary Format:
A = 00111100 , B = 00001101
- If we take the binary value of A and B and make some operations with the help of operators then those operators all are called bitwise operators which are as follows:
- & - Bitwise AND
- | - Bitwise OR
- ~ - Bitwise Complement
- ^ - Bitwise XOR
- << - Left Shift
- >> - Right Shift
5. Assignment Operators:
- Assignment Operators are used to assign values to variables.
- Following are all types of assignment operators given by functionalities and examples. Follow the table below.
6. Conditional Operators:
- The Conditional Operator is also known as the Ternary Operator.
- This operator consists of three operands, i.e. one expression and two statements.
- Syntax: (expression) ? statement1 : statement2 ;
- Firstly 'expression' is tested, if it is true the statement1 will be returned otherwise, statement2 will be returned.
- Example:
b = 2;
c = (a<b) ? a : b ;
Then we get c = 2
7. SizeOf Operator:
- This operator returns the size of a variable.
- Syntax: sizeof ( variable_name );
- Example:
int size;
size = sizeof(a);
Here, a is a float variable, it will return 4.
8. Increment & Decrement Operator:
- The Increment Operator is used to increase some value by 1.
- The Decrement Operator is used to Decrease some value by 1.
- Both are unary operators i.e. they have performed their works with single operands.
- They are symbolized as increments (++) and decrements (--).
- Follow the below table for explanations and examples.
That's all for this section, you can also watch videos on YouTube for more information about this concept.