Operators in C++ – Part 1

Written by

Vaaruni Agarwal

Operators are the symbols that are used to carry out specific arithmetic, logical, mathematical or assignment operation.

Operators work on operands to give a desired result. With the help of the different categories of the operands, one can carry out different operations in a C++ Program. 

The C++ language offers its coders to use different types of operands, these are as follows:

 

  • Arithmetic Operators 
  • Assignment Operators 
  • Relational Operators 
  • Logical Operators 
  • Bitwise Operators 
  • Misc Operators 

 

In this chapter, we will focus on: Arithmetic, Assignment and Relational Operators.

Arithmetic Operators:

The C++ language allows the use of the following Arithmetic Operators, here we have assumed two variables A and B where A has a value of 10 and B has 20. Now, let us take a look at the Arithmetic Operators offered by C++:

Operator  Description  Example 
Adds two operands  A + B will give 30 
–  Subtracts second operand from the first  A – B will give -10 
Multiplies both operands  A * B will give 200 
Divides numerator by de-numerator  B / A will give 2 
Modulus Operator and remainder of after an integer division  B % A will give 0 
++  Increment operator, increases integer value by one  A++ will give 11 
—  Decrement operator, decreases integer value by one  A– will give 9 

 

Here, ++ and the – are known as the unary operators, as they need only one operand to operate while all the other arithmetic operators given in the table are binary operators, because they need two operands to operate on.

To understand the arithmetic operators, in a better way, here is a C++ program that uses all the arithmetic operators:

#include <iostream.h> 

void main() 

int a = 21; 

int b = 10; 

int c ; 

c = a + b; 

cout << c << endl ; 

c = a - b; 

cout  << c << endl ; 

c = a * b; 

cout << c << endl ; 

c = a / b; 

cout << c << endl ; 

c = a % b; 

cout <<  c << endl ; 

c = a++; 

cout  << c << endl ; 

c = b--; 

cout << c << endl ; 

}

Output:

31

11

210

2

1

22

9

Assignment Operators:

C++ offers the use of simple and shorthand assignment operators as well. The Assignment Operators are as follows:

Operator  Description  Example 
Simple Assignment Operator, used to assign values from right side to left side  C= A + B will assign value of A+B to C 
+= Shorthand Assignment operator to add the right operand to left and assign the new value to left operand C+= A will expand to C=C+A 
-=  Shorthand Assignment operator to subtract the right operand to left and assign the new value to left operand C-= A will expand to C=C-A
*= Shorthand Assignment operator to multiply the right operand to left and assign the new value to left operand C*= A will expand to C=C*A
/= Shorthand Assignment operator to divide the right operand to left and assign the new value to left operand C/= A will expand to C=C/A
%= Shorthand Assignment operator to return quotient of the right operand to left and assign the new value to left operand C%= A will expand to C=C%A

 

Relational Operators:

Relational Operators are used to tell the relation between two operands, they return the result as true or false.

Suppose, A=10 and B=20, then the relational operators will be as follows:

Operator  Description  Example 
==  Checks if the values of two operands are equal or not, if yes then condition becomes true.  (A == B) is not true. 
!=  Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.  (A != B) is true. 
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.  (A > B) is not true. 
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.  (A < B) is true. 
>=  Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.  (A >= B) is not true. 
<=  Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.  (A <= B) is true. 

 

Operators in C++ &#8211; Part 1