Introduction to C++ Operators
Operators are symbols that inform the compiler to perform mathematical operations; C++ provides various types of operators like arithmetic, assignment, logical, comparison, and bitwise operators. Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division. Assignment operators are used to assign values to another variable. Comparison operators are used to compare values based on conditions; Logical operators identify the logic between variables.
What are Operators in C++?
The different types of operators used in C++ are as follows:
1. Arithmetic Operators
In C++, arithmetic operators are used to perform arithmetic operations as described below.
Let us take an example of operands a, and b with values 10 and 5, respectively.
Arithmetic Operators in C++ | ||
Operator | Description | Example |
+ | Addition of two operands | a + b will give 15 |
– | Subtraction of the right operand from the left operand | a – b will give 5 |
* | Multiplication of two operands | a * b
will give 50 |
/ | Division of the left operand by the right operand | a / b
will give 2 |
% | Modulus – the remainder of the division of the left operand by the right | a % b will give 0 |
++ | Increment Operator, which increases the value of the operand by 1. | b++ will give 6 |
— | Decrement Operator, which decreases the value of the operand by 1. | b — will give 4 |
2. Relational Operators
The relational operators are used to compare values between operands and return TRUE or FALSE according to the condition specified in the statement.
Relational Operators in C++ | ||
Operator | Description | Example |
> | If the value of the left operand is greater than that of the value of the right operand, the condition becomes true; if not, then false. | a > b |
< | If the value of the left operand is less than that of the value of the right operand, the condition becomes true; if not, then false. | a < b |
== | If both the operands have equal value, the condition becomes true; else false. | a == b |
!= | If both the operands do not have equal value, the condition becomes true; else false. | a != b |
>= | If the value of the left operand is greater than or equal to the right operand, the condition becomes true; if not, then false. | a >= b |
<= | If the value of the left operand is less than or equal to the right operand, the condition becomes true; if not, then false. | a <= b |
Let us assume the value of operands a = 10, and b = 5, and perform various operations to understand the relational operators.
- a > b will give the result TRUE as 10 is greater than 5.
- a < b will give the result FALSE as 10 is greater than 5.
- a == b will give the result FALSE as 10 is not equal to 5.
- a != b will give the result TRUE as 10 is not equal to 5.
- a >= b will give the result TRUE as 10 is greater than 5.
- a <= b will give the result FALSE as 10 is not equal to or less than 5.
3. Logical Operators
The logical operators used in C++ are shown below:
Logical Operators in C++ | ||
Operator | Description | Example |
|| | It is the logical OR Operator. The condition becomes true if any of the two operands are non-zero. | a || b |
&&
|
It is the logical AND Operator. The condition becomes true if both of the two operands are non-zero. | a && b |
!
|
It is the logical NOT operator and reverses the state of the logical operator with which it is used. | !a |
Let us assume the value of operands a = 1, and b = 0 and perform various operations to understand the logical operators.
- a || b will be TRUE as one of the two operands is non-zero.
- a && b will be FALSE as one of the operands is zero.
- !a will be 0 as it reverses the state of the operand.
4. Assignment Operators
The assignment operators used in C++ are shown below.
Assignment Operators in C++ | ||
Operator | Description | Example |
= | This is a simple assignment operator which assigns the value of the right side operand to the left side operand. | x = y will assign the value of y to x. |
+= | This operator performs the addition of the right operand to the left operand, and the result is assigned to the left operand. | x += y is interpreted as x = x + y |
-= | This operator performs subtraction of the right operand from the left operand, and the result is assigned to the left operand. | x -= y is equal to x = x – y |
*= | This operator performs the multiplication of the right operand with the left operand, and the result gets assigned to the left operand. | x *= y is equal to x = x * y |
/= | This operator performs division of the left operand with the right operand, and the result is assigned to the left operand. | x /= y is equal to x = x / y |
%= | This takes the modulus of the two operands, and the result is assigned to the left operand. | x %= y is equal to x = x % y |
>>= | This is a binary right shift and assignment operator. | x >> 5 equals to x = x >> 5 |
<<= | This is a binary left shift and assignment operator. | x << 5 equals to x = x << 5 |
^= | This is called bitwise exclusive OR and assignment operator. | x ^= 5 equals to x = x ^ 5 |
|= | This is called a bitwise OR assignment operator. | x |= 5 equals to x = x | 5 |
&= | This is called bitwise AND assignment operator. | x &= 5 equals to x = x & 5 |
Let us assume the value of x as 5. A few examples of operations were performed using a few assignment operators shown above.
- x = 5 will assign the value 5 to x.
- x += 3 will give the result as x = x +3 i.e. 5+3= 8 will be assigned to x.
- x -=2 will give the result as x = x +3 i.e. 5-2= 3 will be assigned to x.
Recommended Articles
We hope that this EDUCBA information on “C++ Operators” was beneficial to you. You can view EDUCBA’s recommended articles for more information.