C++ Operators and their types

In this tutorial, you will learn C++ Operators and their types with examples. Operators are the primary concept of any programming language, used to develop a foundation in programming for freshers. Operators are symbols that tell the compiler to execute the mathematical operations, C++ presents many types of operators like arithmetic operators, assignment operators, logical operators, comparison operators, and bitwise operators. Arithmetic operators are used to describe mathematical operations like addition, subtraction multiplication, and division. Assignment operators are used to specify values to another variable. Comparison operators are used to analyze values based on conditions, Logical operators know the logic between variables.

What are operators in C++?

The different types of operators used in C++ are the following.

  • Relational Operators
  • Arithmetic Operators
  • Logical Operators
  • Assignment Operators

C++ Relational Operators

C++ Relational operators are also known for comparison operators. Relational operators are used to describing the condition, that is it analyzes the two values and shows the result.

Operators DescriptionExamples
>If the value of x greater than the value of y, the condition will be true, else false.x>y
<If the value of x less than the value of y, the condition will be true, else false.x<y
==if x and y both have equal values the condition will be true, else false.x==y
!=if x and y both don’t have equal values the condition will be true, else false.x!=y
>=if the value of x greater or equal the value of y the condition will be true, else false.x>=y
<=if the value of x less or equal the value of y the condition will be true, else false.x<=y
Relational Operators in C++

Arithmetic operators

C++ Arithmetic operators are used to producing some mathematical operations. Like some other operators, C++ also holds arithmetic operators to show some mathematical operations like addition, subtraction, multiplication, etc.
Let’s hold an example of operands x, y with values 20 and 5 respectively.

OperatorsDescriptionExamples
+Addition  two numbersx+y =25
_Subtraction  two numbersx-y =15
*Multiplication two numbersx*y =100
/Division two numbersx/y =4
%Modulus – the remainder of the divisionx%y =1
++Increment Operator increases the value operands by 1.x++ =21
__Decrement Operator increases the value operands by 1.x– =19
Arithmetic operators in C++

C++ Logical Operators

C++ Logical Operators are used to combine two or more conditions to the evaluation of the initial condition into consideration. The outcomes of the performance of a logical operator is a boolean value both true or false. For example, the AND described as ‘&&’ operator in C++ returns true when both the states under event are satisfied. Otherwise, it returns false. Therefore, x && y returns true when both x and y are true.

OperatorsDescriptionExamples
|| (OR)The condition will be true if any of the two operands is non-zero.x||y
&& (AND)The condition will be true if both of the two operands are non-zero.x&&y
! (NOT)Logical NOT operator and reverses the state of the logical operator with which it is used.!x
Logical Operators in C++

Assignment Operators

C++ Assignment operators are used to allocating value to a variable. The left operand of the assignment operator is a variable and the right operand of the assignment operator is a value. The value on the right operand must be of the same data-type of the variable on the left side otherwise the compiler will build an error.

C++ Assignment Operators

OperatorsDescriptionExamples
=This is an easy operator which assigns the value of the right to left operand.x = y will assign the value of y to x.
+=It’ll perform the addition of the right to left operand and the result will be to the left operand.b += y is interpreted as b = b+ y
-=This operator performs subtraction of the right from the left operand and the result is left operand.c-= y to c = c – y
*=This operator performs multiplication of the right with the left operand and the result gets to the left operand.d*= y to d= d * y
/=It’ll perform division of left with the right operand and the result is to the left operand.m/= n is equal to m= m / n
%=This uses the modulus of the two operands and the result is assigned to the left operand.rt %= y is equal to rt = rt % y
>>=It is a binary right shift and assignment operator.a >> 5 equals to a = a>> 5
<<=This is a binary left shift and assignment operator.x << 5 equals to b = b << 5
^=It is called a bitwise exclusive OR and assignment operator.x ^= 5 equals to c = c^ 5
|=This is called a bitwise OR assignment operator.x |= 5 equals to a = a | 5
&=This is called bitwise AND assignment operator.x &= 5 equals to x = x & 5
C++ Assignment Operators

Leave a Reply

Your email address will not be published. Required fields are marked *