๐ง Operators in C++

4 min read
# 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 | ? : |
0
Subscribe to my newsletter
Read articles from Shrey Vohra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
