Operators In C++
The symbols used to perform arithmetic or logical operations on variables are called operators and the entity on which the operation is performed is called an operand.
The operators based on operands can be classified into two types
- Unary operators
- Binary operators
- Unary operators:
When any operation is performed at that time if only one operand is used then that operator is called as unary operator
Example: consider a variable A and the operation performed is shown below
A = -A ; (Here the “-“sign is used to make the value present in A negative)
- Binary operator:
As the name suggests these operators required two operands to perform the operations
Example: consider A, B, C are the variables then
C = A+B;
There are 7 basic types of operators used in any programming language.
• Arithmetic operators
- Addition (+):
As the name and symbol suggest it is used for simple mathematical addition
Ex. A =10, B = 5
C = A + B
The value of C will be C = 15
- Subtraction (-):
This operator works the same as a mathematical subtraction
A =10, B = 5
C = A - B
Value of C will be C = 5
- Multiplication (*):
This operator works the same as a mathematical multiplication.
Ex. A =10, B = 5
C = A * B
The value of C will be C = 50
- Division (/):
This operator works the same as a mathematical division.
Ex. A =10, B = 5
C = A / B
The value of C will be C = 2
Ex. A =5, B = 2
C = A / B
Value of C if it is an integer type variable then it is C = 2
Value of C if it is a float or double type variable then it is C = 2.5
As we learn in Data types that an integer variable can save a whole number or an integer value only hence division is not completely done in points but if the variable type is float or double then the complete division takes place.
- Modulo Division (%):
This is a very useful operator in any programming language. This operator does not store the quotient as the normal division process does instead it stores the remainder. Ex. A =5, B = 2
C = A / B
The value of C will be C = 1
Ex. A =10, B = 5
C = A / B
The value of C will be C = 0
• Relational operators
- Less than (<):
This operator checks whether a value is less or not. If it is less than the reference it sends a true value(1) to the compiler else it sends a false value (0) to the compiler.
- Greater than (>):
This operator checks whether a value is Greater or not. If it is Greater than the reference it sends a true value(1) to the compiler else it sends a false value (0) to the compiler.
- Less than equal to (<=):
This operator checks whether a value is less or equal. If it is less or equal to the reference it sends a true value(1) to the compiler else it sends a false value (0) to the compiler.
- Greater than equal to (>=):
This operator checks whether a value is greater or equal. If it is greater or equal to the reference it sends a true value(1) to the compiler else it sends a false value (0) to the compiler.
- Equal to (==):
This operator checks whether a value is equal or not. If it is Equal to the reference it sends a true value(1) to the compiler else it sends a false value (0) to the compiler.
- Not equal to (!=):
This operator checks whether a value is equal or not. If it is NOT equal to the reference it sends a true value(1) to the compiler else it sends a false value (0) to the compiler.
• Logical operators
Logical operators work similarly to logical gate the three logical operators are:
- And (&&):
It works similar to the logical AND gate. If both the condition Is true then only it gives true(1) output
- OR (||):
It works similar to the logical OR gate. If anyone of the condition Is true then it gives true(1) output
- Not (!):
It works similar to the logical NOT gate. If the condition Is false then only it gives true(1) output
• Assignment operator
It is used to assign a value to a variable (=). Don’t get confused between assignment and equal to operators. If there is one “=” sign then it is assignment operator and it will assign a new value to the variable and if there is two “==” sign then it will check the value which is previously assigned to the variable
• Increment and Decrement
Ø Increment (++)
Ø Decrement (--)
Increment and Decrement as the name suggests these operators are used to increase or decrease the value of variables. Now it can increase or decrease the value only by 1. Increment and Decrement can be done by two methods which are post and pre-increment/decrement.
- Post increment/decrement:
In this method first the value is assigned or used in operation then increment or decrement is done
- Pre increment/decrement:
In this method first increment or decrement is done then the value is assigned or used in operation
To clear the confusion, we will see some examples:
Let x = 5
y = x++ then value of y = 5 and x = 6
y = ++x then value of y = 6 and x = 6
y = x-- then value of y = 5 and x = 4
y = --x then value of y = 4 and x = 4
• Conditional operator
- Ø Ternary operator
Syntax:
(exp1)?exp2:exp3;
This operator is use when we required to take any action base on a condition. If the condition is true then exp2 is executed else exp3 is executed
Example:
Let x = 5, y = 6, z = 7
( x<y && y<z )? cout << x: cout << y;
Now here we use two expressions and link them with logical AND operator. The first condition is x<y yes because x = 5 and y = 6. Similarly the second condition y<z is also true as y = 6 and z = 7. Since both, the condition is true the logical AND operator gives true output and hence the condition cout << x is executed. Here cout means it tells the compiler to print, and cout << x means print the value of x.
• Bitwise operators
Bitwise operators are used to performing operations on a bit level.
- Ø Bitwise AND (&):
It performs bitwise AND operation
- Ø Bitwise OR (|):
It performs bitwise OR operations
- Ø Bitwise EX-OR (^):
It performs bitwise EX-OR operation. Here EX-OR operation is similar to the EX-OR gate operation.
- Ø Left shift (<<):
This operator will shift the bits towards left by one bit
- Ø Right Shift (>>):
This operator will shift the bits towards the right by one bit
0 Comments