Lesson 18 of 60 – Assignment Operators in C++
30%

Assignment Operators in C++

Assignment operators are used to assign values to variables. The most basic assignment operator is =. C++ also provides compound assignment operators such as +=, -=, *=, /=, and %=.

Note: The assignment operator = assigns a value to a variable. It is different from the equality operator ==, which compares two values.

1. What are Assignment Operators?

Assignment operators are used to store a value in a variable.

int age = 25;

Here, the value 25 is assigned to the variable age.

2. Basic Assignment Operator =

The = operator assigns the value on its right to the variable on its left.

int number;

number = 100;

After the assignment, number contains the value 100.

3. Assigning Values During Declaration

A variable can be declared and assigned a value in the same statement.

int marks = 85;
double price = 99.50;
char grade = 'A';

4. Changing a Variable's Value

The value stored in a variable can be changed using the assignment operator.

int age = 20;

age = 25;

The final value of age is 25.

5. Assigning One Variable to Another

The value of one variable can be assigned to another variable.

int a = 50;
int b;

b = a;

Now both a and b contain the value 50.

6. Assignment from an Expression

An expression can also be assigned to a variable.

int a = 10;
int b = 20;

int sum = a + b;

The value of sum is 30.

7. Addition Assignment +=

The += operator adds a value to the existing value of a variable and stores the result in the same variable.

int number = 10;

number += 5;

This is equivalent to:

number = number + 5;

The final value is 15.

8. Addition Assignment with Variables

int marks = 60;
int bonus = 10;

marks += bonus;

The final value of marks is 70.

9. Subtraction Assignment -=

The -= operator subtracts a value from the current value of a variable.

int number = 20;

number -= 5;

This is equivalent to:

number = number - 5;

The final value is 15.

10. Subtraction Assignment Example

int balance = 1000;

balance -= 250;

The final balance is 750.

11. Multiplication Assignment *=

The *= operator multiplies the current value by another value and stores the result.

int number = 10;

number *= 5;

This is equivalent to:

number = number * 5;

The final value is 50.

12. Multiplication Assignment Example

int price = 100;

price *= 3;

The final value of price is 300.

13. Division Assignment /=

The /= operator divides the current value by another value and stores the result.

int number = 100;

number /= 5;

This is equivalent to:

number = number / 5;

The final value is 20.

14. Division Assignment with Decimal Values

double price = 100.0;

price /= 4;

The final value of price is 25.0.

15. Modulus Assignment %=

The %= operator calculates the remainder and stores it in the same variable.

int number = 17;

number %= 5;

This is equivalent to:

number = number % 5;

The final value is 2.

16. Modulus Assignment Example

int number = 25;

number %= 4;

25 divided by 4 leaves a remainder of 1, so the final value of number is 1.

17. Bitwise AND Assignment &=

The &= operator performs a bitwise AND operation and assigns the result back to the variable.

int a = 6;

a &= 3;

This is equivalent to:

a = a & 3;

18. Bitwise OR Assignment |=

The |= operator performs a bitwise OR operation and assigns the result back to the variable.

int a = 6;

a |= 3;

This is equivalent to:

a = a | 3;

19. Bitwise XOR Assignment ^=

The ^= operator performs a bitwise XOR operation and assigns the result back to the variable.

int a = 6;

a ^= 3;

This is equivalent to:

a = a ^ 3;

20. Left Shift Assignment <<=

The <<= operator shifts the bits of a value to the left and assigns the result back to the variable.

int number = 3;

number <<= 2;

This is equivalent to:

number = number << 2;

21. Right Shift Assignment >>=

The >>= operator shifts the bits of a value to the right and assigns the result back to the variable.

int number = 20;

number >>= 2;

This is equivalent to:

number = number >> 2;

22. Compound Assignment Operators

The following are common compound assignment operators in C++:

Operator Example Equivalent Expression
+= a += b a = a + b
-= a -= b a = a - b
*= a *= b a = a * b
/= a /= b a = a / b
%= a %= b a = a % b

23. Assignment Operators with User Input

#include <iostream>

int main() {

    int number;

    std::cout << "Enter a number: ";
    std::cin >> number;

    number += 10;

    std::cout << "After addition: " << number;

    return 0;
}

The program adds 10 to the number entered by the user.

24. Multiple Assignment Operations

Several assignment operations can be performed one after another.

int number = 100;

number += 20;
number -= 10;
number *= 2;
number /= 5;

Each operation changes the current value of number.

25. Assignment Operators with Different Data Types

Assignment can involve different numeric types, but conversions may occur.

double price = 99.75;

int amount = price;

The conversion from double to int discards the fractional part, so amount becomes 99.

26. Assignment Operators in a Marks Example

#include <iostream>

int main() {

    int marks = 60;

    marks += 10;
    marks -= 5;

    std::cout << "Final Marks: " << marks;

    return 0;
}

First 10 marks are added and then 5 marks are subtracted. The final value is 65.

27. Assignment Operators in a Calculator

#include <iostream>

int main() {

    double amount = 1000;

    amount += 500;
    amount -= 200;
    amount *= 2;
    amount /= 2;

    std::cout << "Final Amount: " << amount;

    return 0;
}

28. Complete Assignment Operators Example

#include <iostream>

int main() {

    int number = 20;

    std::cout << "Initial: "
              << number << std::endl;

    number += 10;
    std::cout << "After += 10: "
              << number << std::endl;

    number -= 5;
    std::cout << "After -= 5: "
              << number << std::endl;

    number *= 2;
    std::cout << "After *= 2: "
              << number << std::endl;

    number /= 5;
    std::cout << "After /= 5: "
              << number << std::endl;

    number %= 3;
    std::cout << "After %= 3: "
              << number << std::endl;

    return 0;
}

29. Common Assignment Operator Mistakes

  • Confusing = with ==.
  • Forgetting that assignment changes the value of a variable.
  • Using division by zero with /=.
  • Using %= with floating-point operands.
  • Forgetting that integer division can discard the fractional part.
  • Using compound assignment without understanding the current value of the variable.
  • Ignoring possible type conversions during assignment.

30. Assignment Operators – Final Summary

Operator Name Example Equivalent
= Assignment a = b Assign b to a
+= Addition assignment a += b a = a + b
-= Subtraction assignment a -= b a = a - b
*= Multiplication assignment a *= b a = a * b
/= Division assignment a /= b a = a / b
%= Modulus assignment a %= b a = a % b
&= Bitwise AND assignment a &= b a = a & b
|= Bitwise OR assignment a |= b a = a | b
^= Bitwise XOR assignment a ^= b a = a ^ b
<<= Left shift assignment a <<= b a = a << b
>>= Right shift assignment a >>= b a = a >> b

📌 Key Points

  • The = operator assigns a value to a variable.
  • += adds a value to the existing variable.
  • -= subtracts a value from the existing variable.
  • *= multiplies the existing variable by a value.
  • /= divides the existing variable by a value.
  • %= stores the remainder of a division operation.
  • C++ also provides bitwise compound assignment operators.
  • Compound assignment operators are shorter forms of common assignment expressions.
  • Do not confuse = with ==.
  • Be careful about data-type conversions during assignment.

🧠 Quick Quiz

Question: Which operator adds a value to an existing variable and assigns the result back to that variable?