Cpp Operators

C++ Operators:

1. Arithmetic Operators:

+ (Addition): Adds two values together.

        
int a = 5, b = 3;
int sum = a + b;  // sum is now 8
        
    

- (Subtraction): Subtracts the right operand from the left operand.

        
int a = 10, b = 4;
int difference = a - b;  // difference is now 6
        
    

* (Multiplication): Multiplies two values.

        
int a = 7, b = 2;
int product = a * b;  // product is now 14
        
    

/ (Division): Divides the left operand by the right operand.

        
int a = 20, b = 4;
int result = a / b;  // result is now 5
        
    

% (Modulus): Computes the remainder of the division of the left operand by the right operand.

        
int a = 17, b = 5;
int remainder = a % b;  // remainder is 2
        
    

2. Relational Operators:

== (Equal to): Checks if two values are equal.

        
int a = 5, b = 5;
bool isEqual = (a == b);  // isEqual is true
        
    

!= (Not equal to): Checks if two values are not equal.

        
int a = 6, b = 5;
bool isNotEqual = (a != b);  // isNotEqual is true
        
    

< (Less than): Checks if the left operand is less than the right operand.

        
int a = 3, b = 5;
bool isLess = (a < b);  // isLess is true
        
    

> (Greater than): Checks if the left operand is greater than the right operand.

        
int a = 8, b = 5;
bool isGreater = (a > b);  // isGreater is true
        
    

<= (Less than or equal to): Checks if the left operand is less than or equal to the right operand.

        
int a = 5, b = 5;
bool isLessOrEqual = (a <= b);  // isLessOrEqual is true
        
    

>= (Greater than or equal to): Checks if the left operand is greater than or equal to the right operand.

        
int a = 6, b = 5;
bool isGreaterOrEqual = (a >= b);  // isGreaterOrEqual is true
        
    

3. Logical Operators:

&& (Logical AND): Returns true if both operands are true.

        
bool x = true, y = false;
bool result = x && y;  // result is false
        
    

|| (Logical OR): Returns true if at least one of the operands is true.

        
bool x = true, y = false;
bool result = x || y;  // result is true
        
    

! (Logical NOT): Inverts the value of the operand.

        
bool x = true;
bool result = !x;  // result is false
        
    

4. Assignment Operators:

= (Assignment): Assigns the value on the right to the variable on the left.

        
int a = 10;
int b = 5;
a = b;  // 'a' is now 5
        
    

+= (Add and assign): Adds the right operand to the left operand and stores the result in the left operand.

        
int a = 5;
int b = 3;
a += b;  // 'a' is now 8
        
    

-= (Subtract and assign): Subtracts the right operand from the left operand and stores the result in the left operand.

        
int a = 10;
int b = 4;
a -= b;  // 'a' is now 6
        
    

5. Increment and Decrement Operators:

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

        
int a = 5;
a++;  // 'a' is now 6
        
    

-- (Decrement): Decreases the value of a variable by 1.

        
int a = 5;
a--;  // 'a' is now 4
        
    

6. Bitwise Operators:

C++ provides various bitwise operators like &, |, ^, ~, <<, and >> for bitwise manipulation. These are often used for low-level bit-level operations.

7. Conditional (Ternary) Operator:

? : (Conditional Operator): Used for a concise way of expressing an if-else statement.

        
int a = 5, b = 3;
int max = (a > b) ? a : b;  // max is 5
        
    

8. Comma Operator:

, (Comma Operator): Used to separate expressions and evaluate them left to right.

        
int a = 5, b = 10, c;
c = (a++, b++);  // 'c' gets the value of 'b', and 'a' and 'b' are incremented
        
    

9. Member Access Operators:

. (Dot Operator): Accesses a member of an object (used with structures and classes).

        
struct Point {
    int x, y;
};

Point p;
p.x = 5;  // Using dot operator
        
    

-> (Arrow Operator): Accesses a member of an object through a pointer to an object.

        
struct Point {
    int x, y;
};

Point p;
Point* ptr = &p;
ptr->y = 10;  // Using arrow operator