C++ Operators

In C++, operators are special symbols that are used to perform operations on variables or values. There are various types of operators in C++, including:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Conditional Operator

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on variables. The basic arithmetic operators in C++ are addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

Here’s an example:

int x = 10;
int y = 3;
int z = x + y; // z = 13
int a = x * y; // a = 30
int b = x % y; // b = 1 (modulus gives the remainder after division)

Assignment Operators

Assignment operators are used to assign values to variables. The basic assignment operator in C++ is the equal sign (=).

Here’s an example:

int x = 10;
x += 5; // equivalent to x = x + 5, so x = 15

Comparison Operators

Comparison operators are used to compare values. The basic comparison operators in C++ are less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equal to (==), and not equal to (!=).

Here’s an example:

int x = 10;
int y = 5;
bool result = x > y; // result = true

Logical Operators

Logical operators are used to combine boolean expressions. The basic logical operators in C++ are and (&&), or (||), and not (!).

Here’s an example:

bool a = true;
bool b = false;
bool result = a && b; // result = false

Bitwise Operators

Bitwise operators are used to perform operations on the binary representations of values. The basic bitwise operators in C++ are and (&), or (|), not (~), exclusive or (^), left shift (<<), and right shift (>>).

Here’s an example:

int x = 5; // binary representation: 0101
int y = 3; // binary representation: 0011
int result = x & y; // result = 1 (binary representation: 0001)

Conditional Operator

The conditional operator ‘? :’ is a ternary operator that evaluates a condition and returns one of two values, depending on whether the condition is true or false.

Here’s an example:

int x = 10;
int y = 5;
int result = (x > y) ? x : y; // result = 10
Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial