Skip to main content

Operators in C++

Hey there! In this guide, we'll explore operators in C++. Operators are symbols that instruct the compiler to perform specific operations on variables or values. C++ supports a variety of operators, including arithmetic, relational, logical, bitwise, and more. Let's dive in!

  • Operators are symbols that instruct the compiler to perform specific operations on variables or values.
  • C++ supports a variety of operators, including arithmetic, relational, logical, bitwise, and more.

1. Arithmetic Operators​

Arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, and division.

OperatorDescriptionExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulus (remainder)x % y

Example:​

int x = 10, y = 5;
cout << (x + y); // Output: 15
cout << (x - y); // Output: 5
cout << (x * y); // Output: 50
cout << (x / y); // Output: 2
cout << (x % y); // Output: 0

2. Relational Operators​

Relational operators compare two values and return a boolean result (true or false).

OperatorDescriptionExample
==Equal tox == y
!=Not equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Example:​

int x = 10, y = 5;
cout << (x == y); // Output: false
cout << (x != y); // Output: true
cout << (x > y); // Output: true
cout << (x < y); // Output: false
cout << (x >= y); // Output: true
cout << (x <= y); // Output: false

3. Logical Operators​

Logical operators are used to perform logical operations and combine multiple conditions.

OperatorDescriptionExample
&&Logical AND(x > 5 && y < 10)
!Logical NOT!(x > 5)

Example:​

int x = 10, y = 5;
cout << (x > 5 && y < 10); // Output: 1 (true)
cout << (x > 5 || y > 10); // Output: 1 (true)
cout << !(x > 5); // Output: 0 (false)

4. Assignment Operators​

Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Assigns valuex = y
+=Adds and assignsx += y
-=Subtracts and assignsx -= y
*=Multiplies and assignsx *= y
/=Divides and assignsx /= y
%=Modulus and assignsx %= y

Example:​

int x = 10, y = 5;
x += y; // Equivalent to x = x + y
cout << x; // Output: 15

5. Increment and Decrement Operators​

Increment and decrement operators are used to increase or decrease a variable's value by 1.

OperatorDescriptionExample
++Increments value++x or x++
--Decrements value--x or x--

Example:​

int x = 10;
cout << ++x; // Output: 11 (Pre-increment)
cout << x--; // Output: 11 (Post-decrement)
cout << x; // Output: 10

6. Bitwise Operators​

Bitwise operators operate on bits and perform bit-level operations.

OperatorDescriptionExample
&Bitwise ANDx & y
Bitwise OR
^Bitwise XORx ^ y
~Bitwise NOT~x
>>Left shiftx >> 2
<<Right shiftx << 2

Example:​

int x = 10;
int x = 5, y = 9;
cout << (x & y); // Output: 1
cout << (x | y); // Output: 13

7. Ternary Operator​

The ternary operator is a shorthand for an if-else statement.

OperatorDescriptionExample
?:Ternarycondition ? expr1 : expr2

Example:​

int x = 10;
int result = (x > 5) ? 100 : 200;
cout << result; // Output: 100

Understanding these operators is key to mastering C++ programming and writing efficient code!