🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

C++ Operators

C++ Operators Overview

1. Arithmetic Operators:

These operators perform basic arithmetic operations.


+ (Addition): Adds two operands.
- (Subtraction): Subtracts the right operand from the left operand.
* (Multiplication): Multiplies two operands.
/ (Division): Divides the left operand by the right operand.
% (Modulus): Returns the remainder of the division of the left operand by the right operand.

Example:


int a = 5, b = 2;
int sum = a + b;        // sum = 7
int difference = a - b; // difference = 3
int product = a * b;    // product = 10
int quotient = a / b;   // quotient = 2
int remainder = a % b;  // remainder = 1

2. Relational Operators:

These operators are used to compare two values.


== (Equal to): Checks if two operands are equal.
!= (Not equal to): Checks if two operands are not equal.
> (Greater than): Checks if the left operand is greater than the right operand.
< (Less than): Checks if the left operand is less than the right operand.
>= (Greater than or equal to): Checks if the left operand is greater than or equal to the right operand.
<= (Less than or equal to): Checks if the left operand is less than or equal to the right operand.

Example:


int x = 5, y = 10;
bool isEqual = (x == y);           // false
bool isNotEqual = (x != y);        // true
bool isGreater = (x > y);         // false
bool isLess = (x < y);            // true
bool isGreaterOrEqual = (x >= y);  // false
bool isLessOrEqual = (x <= y);     // true

3. Logical Operators:

These operators perform logical operations.


&& (Logical AND): Returns true if both operands are true.
|| (Logical OR): Returns true if at least one operand is true.
! (Logical NOT): Returns true if the operand is false and vice versa.

Example:


bool condition1 = true, condition2 = false;
bool resultAnd = (condition1 && condition2);  // false
bool resultOr = (condition1 || condition2);        // true
bool resultNot = !condition1;                      // false

4. Assignment Operators:

These operators assign values to variables.


= (Assignment): Assigns the value of the right operand to the left operand.
+=, -=, *=, /=, %= (Compound Assignment): Performs the operation on the variables and assigns the result to the left operand.

Example:


int a = 5, b = 2;
a += b;  // Equivalent to a = a + b;  // a = 7
a *= 3;  // Equivalent to a = a * 3;  // a = 21

5. Increment and Decrement Operators:

++ (Increment): Increases the value of the operand by 1.


int counter = 10;
counter++;  // Increment by 1  // counter = 11
counter--;  // Decrement by 1  // counter = 10

6. Conditional (Ternary) Operator:

? : (Conditional Operator): Provides a concise way of writing an if-else statement.


int x = 5, y = 10;
int result = (x > y) ? x : y;  // result = 10

7. Bitwise Operators:

These operators perform bitwise operations on integers.


& (Bitwise AND)
| (Bitwise OR)
^ (Bitwise XOR)
~ (Bitwise NOT)
<< (Left Shift)
>> (Right Shift)

Example:


int a = 5, b = 3;
int bitwiseAnd = a & b;  // 1
int bitwiseOr = a | b;    // 7
int bitwiseXor = a ^ b;   // 6
int bitwiseNot = ~a;      // -6

These are the fundamental operators in C++. Understanding how to use them is essential for writing effective and expressive code. Keep in mind the order of precedence and associativity when using multiple operators in a single expression.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.