Skip to main content

Command Palette

Search for a command to run...

🔧 Operators in C++

Updated
•4 min read•View as Markdown

# 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.

OperatorDescriptionExampleResult
+Additiona + bSum
-Subtractiona - bDifference
*Multiplicationa * bProduct
/Divisiona / bQuotient
%Modulus (remainder)a % bRemainder

🔄 2. Increment and Decrement Operators

OperatorDescriptionExampleResult
++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).

OperatorDescriptionExampleResult
==Equal toa == btrue if equal
!=Not equal toa != btrue if not equal
>Greater thana > btrue if a > b
<Less thana < btrue if a < b
>=Greater or equala >= btrue if a ≥ b
<=Less or equala <= btrue if a ≤ b

🧠 4. Logical Operators

Used to combine multiple conditions.

OperatorDescriptionExampleResult
&&Logical ANDa > 5 && b < 10true if both are true
``Logical OR
!Logical NOT (negation)!(a == b)true if a != b

📦 5. Assignment Operators

Assign values to variables.

OperatorDescriptionExampleEquivalent To
=Assigna = 5—
+=Add and assigna += 3a = a + 3
-=Subtract and assigna -= 2a = a - 2
*=Multiply and assigna *= 4a = a * 4
/=Divide and assigna /= 2a = a / 2
%=Modulus and assigna %= 2a = a % 2

🧠 6. Bitwise Operators

Operate on binary representations.

OperatorDescriptionExampleResult
&ANDa & bBitwise AND
``OR`a
^XORa ^ bBitwise XOR
~NOT (1’s complement)~aBitwise NOT
<<Left shifta << 1Shifts bits left
>>Right shifta >> 1Shifts bits right

🔀 7. Conditional (Ternary) Operator

A compact way to write if-else.

condition ? value_if_true : value_if_false;

📌 Summary Table

CategoryOperators
Arithmetic+, -, *, /, %
Relational==, !=, <, >, <=, >=
Logical&&, `
Assignment=, +=, -=, *=, /=, %=
Bitwise&, `
Unary++, --
Ternary? :