Introduction to C++ Operators
Operators are symbols that inform the compiler to perform the mathematical operations; C++ provides various types of operators like arithmetic operators, assignment operators, logical operators, comparison operators, and bitwise operators. Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, division. Assignment operators are used to assigning values to another variable. Comparison operators are used to comparing 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 following:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
1. Arithmetic Operators
In C++, arithmetic operators are used to performing arithmetic operations as described below.
Let us take an example of operands a, 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 right operand from the left operand | a – b will give 5 |
* | Multiplication of two operands | a * b
will give 50 |
/ | Division of left operand by the right operand | a / b
will give 2 |
% | Modulus – the remainder of the division of 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; if not, then false. | a == b |
!= | If both the operands do not have equal value, the condition becomes true; if not then 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, 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 result FALSE as 10 is greater than 5.
- a == b will give 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 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, 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 as 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. 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
This has been a guide to C++ Operators. Here we have discussed the different types of operators used in C++ with their syntax and examples. You may also look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses