🔧 Operators in C++
# Operators
In C++, operators are special symbols used to perform operations on variables and values. Operators in C++ are categorized based on the kind of operation they perform.
🧮 1. Arithmetic Operators
Used for basic mathematical operations.
| Operator | Description | Example | Result |
+ | Addition | a + b | Sum |
- | Subtraction | a - b | Difference |
* | Multiplication | a * b | Product |
/ | Division | a / b | Quotient |
% | Modulus (remainder) | a % b | Remainder |
🔄 2. Increment and Decrement Operators
| Operator | Description | Example | Result |
++ | Increment by 1 | ++a or a++ | a = a + 1 |
-- | Decrement by 1 | --a or a-- | a = a - 1 |
++a: Pre-increment (increments first, then uses the value)[pehle increment karo fir istemaal karo]a++: Post-increment (uses the value, then increments)[pehle istemaal karo fir increment karo]
🧱 3. Relational (Comparison) Operators
Used to compare two values; returns a boolean (true or false).
| Operator | Description | Example | Result |
== | Equal to | a == b | true if equal |
!= | Not equal to | a != b | true if not equal |
> | Greater than | a > b | true if a > b |
< | Less than | a < b | true if a < b |
>= | Greater or equal | a >= b | true if a ≥ b |
<= | Less or equal | a <= b | true if a ≤ b |
🧠4. Logical Operators
Used to combine multiple conditions.
| Operator | Description | Example | Result |
&& | Logical AND | a > 5 && b < 10 | true if both are true |
| ` | ` | Logical OR | |
! | Logical NOT (negation) | !(a == b) | true if a != b |
📦 5. Assignment Operators
Assign values to variables.
| Operator | Description | Example | Equivalent To |
= | Assign | a = 5 | — |
+= | Add and assign | a += 3 | a = a + 3 |
-= | Subtract and assign | a -= 2 | a = a - 2 |
*= | Multiply and assign | a *= 4 | a = a * 4 |
/= | Divide and assign | a /= 2 | a = a / 2 |
%= | Modulus and assign | a %= 2 | a = a % 2 |
🧠6. Bitwise Operators
Operate on binary representations.
| Operator | Description | Example | Result |
& | AND | a & b | Bitwise AND |
| ` | ` | OR | `a |
^ | XOR | a ^ b | Bitwise XOR |
~ | NOT (1’s complement) | ~a | Bitwise NOT |
<< | Left shift | a << 1 | Shifts bits left |
>> | Right shift | a >> 1 | Shifts bits right |
🔀 7. Conditional (Ternary) Operator
A compact way to write if-else.
condition ? value_if_true : value_if_false;
📌 Summary Table
| Category | Operators |
| Arithmetic | +, -, *, /, % |
| Relational | ==, !=, <, >, <=, >= |
| Logical | &&, ` |
| Assignment | =, +=, -=, *=, /=, %= |
| Bitwise | &, ` |
| Unary | ++, -- |
| Ternary | ? : |