An Introduction To C++ Operators

As the name suggests, operators are a feature which represent actions. For example, the + operator will add two integers together, or an OR operator will perform comparison actions.
In this article, you will learn about the following commonly used operators:
1) Basic Arithmetic Operators
2) Assignment Operators
3) Increment and decrement Operators
4) Logical Operators
5) Comparison (relational) operators

Basic Arithmetic Operators

As the name suggests, they are used to perform mathematical operations.
Here is a code sample which demonstrates their use-case:

#include iostream;

using namespace std;
int main() {
  int num1 = 2;
  int num2 = 10;
  int additionResult = num1 + num2; //add these numbers
  int subtractionResult = num1 - num2; //subtract these numbers
  int multiplicationResult = num1 * num2;
  int divisionResult = num1 / num2;
  int modulusResult = num1 % num2; //gets the remainder when num1 is divided by num2.
  cout << "num1 + num2: " << additionaResult << endl;
  cout << "num1 - num2: " << subtractionResult << endl;
  cout << "num1 * num2: " << multiplicationResult << endl;
  cout << "num1 / num2: " << divisionResult << endl;
  cout << "num1 % num2: " << modulusResult << endl;
  return 0;
}

This will be the result:

Assignment Operators

They are crucial for assigning values to our variables.

#include iostream;

using namespace std;
int main() {
  int num1 = 20;
  int num2 = 4;
  num2 = num1; //here, num2's value is now num1. 
  //this means that num2 is now 20. 
  cout << "= Output: " << num2 << endl;
  num2 += num1; //this means that num2 = num1 + num2
  cout <<"+= Output: "<< num2 << endl;
  num2 -= num1; //num2 = num2-num1
  cout << "-= Output: " << num2 << endl;
  num2 *= num1; //num2 = num2*num1;\  
  cout << "*= Output: "<< num2 << endl;
  num2 /= num1; //num2 = num2/num1
  cout << "/= Output: " << num2 << endl;
  num2 %= num1; //num2 = num2 % num1.
  cout << "%= Output: " << num2 << endl;
  return 0;
}

Notice that all of these operators are identical to their arithmetic counterparts.

Increment and Decrement Operators

As the name suggests, they are used to increment or decrement the value of a variable.
This snippet demonstrates the usage of this operator:

#include iostream;

using namespace std;
int main() {
  int num1 = 2;
  int num2 = 40;
  cout << "The value of num1 " << num1 << endl;
  cout << "The value of num2 " << num2 << endl;
  num1++; //this will increment num1
  num2--; //this will decrement num2
  cout << "num1++ is: " << num1 << endl;
  cout << "num2-- is: " << num2 << endl;
  return 0;
}

Logical Operators

They are used with bool (Boolean) values where conditional statements are needed.
It follows the following syntax:

bool b1;
bool b2;
b1 && b2 //if both b1 and b2 are true, then this condition is true. Otherwise false.
b1 || b2 //if either b1 or b2 is true, then this condition is true.
!b1 //flips the value of b1. For example, if b1 is false, then this expression will return true.

Here is a brief example of logical operators in action:

#include iostream;

using namespace std;
int main() {
  bool b1 = true;
  bool b2 = false;
  bool andResult = b1 && b2;
  bool orResult = b1 || b2;
  cout << "b1 AND b2: " << andResult << endl;
  cout << "b1 || b2: " << orResult << endl;
  return 0;
}

Comparison Operator

There are six comparison operators in the C++ language:

  • == returns true if both the left side and right side are equal
  • != returns true if both conditions are different.
  • > returns true if the first value is greater than right.
  • < returns true if the first value is less than the right value.
  • >= returns true if the left value is greater than or equal to right side.
  • <= returns true if the left value is less than or equal to the right value.

Here is an example that uses if and else statements for making comparisons:

#include iostream;

using namespace std;
int main() {
  int num1 = 2;
  int num2 = 40;
  //is num1 equal to num2?
  if (num1 == num2) {
    cout << "num1 and num2 are equal" << endl;
  } else {
    cout << "num1 and num2 are not equal" << endl;
  }
  //is num1 NOT equal to num2?
  if (num1 != num2) {
    cout << "num1 and num2 are not equal" << endl;
  } else {
    cout << "num1 and num2 are equal" << endl;
  }
  //is num1 greater than num2?
  if (num1 > num2) {
    cout << "num1 is greater than num2" << endl;
  } else {
    cout << "num1 is not greater than num2" << endl;
  }
  return 0;
}

Conclusion

In this article, you learned about basic operators in the C++ ecosystem. If you encountered any difficulty, I advise you to play with the code so that you can understand its inner workings.
Thank you so much for reading! Happy coding!

Scroll to Top