100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Assignment Operators in C++

Learn C++ assignment operators including =, +=, -=, *=, /=, and %=, and how compound assignment shortens common update expressions.

OperatorsBeginner5 min readJul 7, 2026
Analogies

1. Intro

Assignment operators store a value into a variable. The basic assignment operator is =. C++ also provides compound assignment operators — +=, -=, *=, /=, %=, and bitwise variants &=, |=, ^=, <<=, >>= — which combine an arithmetic or bitwise operation with assignment in one step.

🏏

Cricket analogy: Updating a batsman's score with runs += 4 after a boundary is a compound assignment shortcut, just as C++ provides +=, -=, *=, /=, %= and bitwise &=, |=, ^=, <<=, >>= to combine an operation with storing the result in one step.

2. Syntax

cpp
x = value;      // simple assignment
x += value;     // x = x + value
x -= value;     // x = x - value
x *= value;     // x = x * value
x /= value;     // x = x / value
x %= value;     // x = x % value

3. Explanation

x += value is shorthand for x = x + value, and similarly for the other compound operators. These are not just cosmetic shortcuts — they can be more efficient for complex left-hand expressions, since x is only evaluated once. Assignment expressions themselves evaluate to the assigned value, which is why chained assignment like a = b = c = 5; works: it evaluates right to left, so c = 5 returns 5, which is then assigned to b, and so on.

🏏

Cricket analogy: Writing totalRuns += six evaluates the scorer's running total variable only once, unlike manually writing totalRuns = totalRuns + six twice; and chained scoring like teamA = teamB = teamC = 0 at the start of a tournament resets all three right to left in one line.

Assignment (=) is right-associative, and an assignment expression evaluates to the newly assigned value. This is why a = b == c means a = (b == c)== binds tighter than =, so a receives the boolean result of comparing b and c, not the value of c.

4. Example

cpp
#include <iostream>
using namespace std;

int main() {
    int x = 10;
    x += 5;
    cout << "x += 5 -> " << x << endl;
    x -= 3;
    cout << "x -= 3 -> " << x << endl;
    x *= 2;
    cout << "x *= 2 -> " << x << endl;
    x /= 4;
    cout << "x /= 4 -> " << x << endl;
    x %= 4;
    cout << "x %= 4 -> " << x << endl;
    return 0;
}

5. Output

text
x += 5 -> 15
x -= 3 -> 12
x *= 2 -> 24
x /= 4 -> 6
x %= 4 -> 2

6. Key Takeaways

  • = performs simple assignment; compound operators like +=, -=, *=, /=, %= combine an operation with assignment.
  • x += value is equivalent to x = x + value, evaluating x only once.
  • Assignment is right-associative, enabling chains like a = b = c = 5;.
  • An assignment expression evaluates to the assigned value, which is why it can be nested inside other expressions.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#AssignmentOperatorsInC#Assignment#Operators#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep