C++ Operators

C++ Operators

C++ Operators

In C++, an operators is a symbol or sequence of symbols that performs an operation on one or more operands. There are many types of operators in C++, including arithmetic operators, comparison operators, logical operators, assignment operators, bitwise operators, and more.

Here are some examples of operators in C++:

  • Arithmetic operators: + (addition), – (subtraction), * (multiplication), / (division), % (modulus)
  • Comparison operators: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to)
  • Logical operators: && (logical AND), || (logical OR), ! (logical NOT)
  • Assignment operators: = (simple assignment), += (add and assign), -= (subtract and assign), *= (multiply and assign), /= (divide and assign), %= (modulus and assign)
  • Bitwise operators: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), >> (right shift)

Example:

				
					#include<iostream>

using namespace std;

int main() {

  int num1 = 10, num2 = 5;

  float f1 = 3.14, f2 = 2.71;

  bool b1 = true, b2 = false;

  //Arithmetic operators

  cout << "num1 + num2 = "<< num1 + num2 << endl;

  cout << "num1 - num2 = "<< num1 - num2 << endl;

  cout << "num1 * num2 = "<< num1 * num2 << endl;

  cout << "num1 / num2 = "<< num1 / num2 << endl;

  cout << "f1 + f2 = " <<f1 + f2 << endl;

  cout << "f1 - f2 = " <<f1 - f2 << endl;

  cout << "f1 * f2 = " <<f1 * f2 << endl;

  cout << "f1 / f2 = " <<f1 / f2 << endl;

  // Relational operators

  cout << "num1 > num2 = "<< (num1 > num2) << endl;

  cout << "num1 < num2 = "<< (num1 < num2) << endl;

  cout << "num1 >= num2 = "<< (num1 >= num2) << endl;

  cout << "num1 <= num2 = "<< (num1 <= num2) << endl;

  cout << "f1 > f2 = "<< (f1 > f2) << endl;

  cout << "f1 < f2 = "<< (f1 < f2) << endl;

  cout << "f1 >= f2 = "<< (f1 >= f2) << endl;

  cout << "f1 <= f2 = "<< (f1 <= f2) << endl;

  // Logical operators

  cout << "b1 && b2 = "<< (b1 && b2) << endl;

  cout << "b1 || b2 = "<< (b1 || b2) << endl;

  cout << "!b1 = " << !b1<< endl;

  // Increment and decrement operators

  cout << "num1++ = " <<num1++ << endl;

  cout << "num1 = " <<num1 << endl;

  cout << "++num1 = " <<++num1 << endl;

  cout << "num1-- = " <<num1-- << endl;

  cout << "num1 = " <<num1 << endl;

 cout << "--num1 = " <<--num1 << endl;

 //Bitwise operators

  cout << "num1 & num2 = "<< (num1 & num2) << endl;

  cout << "num1 ^ num2 = "<< (num1 ^ num2) << endl;

 cout << "num1 << 1 = "<< (num1 << 1) << endl;

  return 0;
				
			
Join To Get Our Newsletter
Spread the love