C++ Assignment Operators

πŸ“ C++ Assignment Operators

Assignment Operators ka use variable ko value assign karne aur update karne ke liye hota hai.


πŸ”Ή 1. Basic Assignment (=)

int a = 10;

πŸ”Ή 2. List of Assignment Operators

OperatorExampleMeaning
=a = 5Assign value
+=a += 3a = a + 3
-=a -= 2a = a - 2
*=a *= 4a = a * 4
/=a /= 2a = a / 2
%=a %= 3a = a % 3

πŸ”Ή 3. Examples

βž• += (Addition Assignment)

int x = 10;
x += 5;
cout << x;

Output: 15


βž– -= (Subtraction Assignment)

int x = 10;
x -= 3;
cout << x;

Output: 7


βœ–οΈ *= (Multiplication Assignment)

int x = 4;
x *= 5;
cout << x;

Output: 20


βž— /= (Division Assignment)

int x = 20;
x /= 4;
cout << x;

Output: 5


πŸ” %= (Modulus Assignment)

int x = 10;
x %= 3;
cout << x;

Output: 1


πŸ”Ή 4. Combined Example Program

#include <iostream>
using namespace std;
int main() {
int a = 20;

a += 5; // 25
a -= 3; // 22
a *= 2; // 44
a /= 4; // 11
a %= 5; // 1

cout << a;
return 0;
}


❌ Common Mistake

if (a = 5) // ❌ assignment, not comparison

βœ” Correct:

if (a == 5)

πŸ“Œ Summary

  • Assignment operators value assign & update karte hain

  • Short & readable code likhne mein help karte hain

  • %= sirf integer types ke liye

You may also like...