Assignment and increment operators are used to assign values to variables and modify them efficiently. They are essential in loops, calculations, and program logic. These operators make programming easier by combining operations with assignment.

Assignment Operators

Assignment operators are used to store values in variables. The simplest assignment operator is =. Other assignment operators combine arithmetic operations with assignment, such as += and -=.

The Basic Assignment Operator (=)

  • The = operator assigns a value on the right-hand side to a variable on the left-hand side.

 
#include <iostream>
using namespace std;
int main() {
int a;
a = 10; // Assign 10 to a
cout << "Value of a: " << a << endl;
return 0;
}

Output:

 
Value of a: 10
  • Here, 10 is assigned to variable a.

Compound Assignment Operators (+=, -=)

  • These operators perform an arithmetic operation and assignment in one step.

Using += (Addition Assignment)

  • a += b is equivalent to a = a + b.

 
#include <iostream>
using namespace std;
int main() {
int a = 5;
a += 3; // a = a + 3
cout << "Value of a after += : " << a << endl;
return 0;
}

Output:

 
Value of a after += : 8
  • Saves typing and makes code cleaner.

Using -= (Subtraction Assignment)

  • a -= b is equivalent to a = a - b.

 
#include <iostream>
using namespace std;
int main() {
int a = 10;
a -= 4; // a = a - 4
cout << "Value of a after -= : " << a << endl;
return 0;
}

Output:

 
Value of a after -= : 6
  • Similar operators exist for multiplication and division:

    • *=a *= b (a = a * b)

    • /=a /= b (a = a / b)

    • %=a %= b (a = a % b)

Advantages of Assignment Operators

  1. Simplifies code – fewer characters to type.

  2. Avoids repetition – no need to write variable name twice.

  3. Useful in loops – e.g., accumulating a sum:

 
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for(int i = 1; i <= 5; i++) {
sum += i; // sum = sum + i
}
cout << "Sum: " << sum << endl;
return 0;
}

Output:

 
Sum: 15

Increment and Decrement Operators

Increment (++) and decrement (--) operators are used to increase or decrease a variable by 1. They are very common in loops and counters.

Increment Operator (++)

  • Syntax: ++variable (pre-increment) or variable++ (post-increment)

  • Increases the value of the variable by 1.

Pre-increment (++variable)

  • The variable is incremented first, then used in the expression.

 
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = ++a; // a is incremented first, then assigned to b
cout << "a: " << a << ", b: " << b << endl;
return 0;
}

Output:

 
a: 6, b: 6

Post-increment (variable++)

  • The variable is used first, then incremented.

 
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = a++; // a is assigned to b first, then incremented
cout << "a: " << a << ", b: " << b << endl;
return 0;
}

Output:

 
a: 6, b: 5

Decrement Operator (--)

  • Decreases the value of a variable by 1.

  • Pre-decrement: --variable → decrements first, then uses the value

  • Post-decrement: variable-- → uses the value first, then decrements

 
#include <iostream>
using namespace std;
int main() {
int a = 5;
cout << "--a: " << --a << endl; // 4
cout << "a--: " << a-- << endl; // prints 4, then a becomes 3
cout << "Value of a now: " << a << endl; // 3
return 0;
}

Output:

 
--a: 4
a--: 4
Value of a now: 3

Using Increment/Decrement in Loops

  • Very common in for loops to control the loop counter.

 
#include <iostream>
using namespace std;
int main() {
for(int i = 1; i <= 5; i++) {
cout << "Counter: " << i << endl;
}
return 0;
}
  • Here, i++ increments i after each iteration.

  • You can also count backwards using --:

 
#include <iostream>
using namespace std;
int main() {
for(int i = 5; i >= 1; i--) {
cout << "Counter: " << i << endl;
}
return 0;
}

Output:

 
Counter: 5
Counter: 4
Counter: 3
Counter: 2
Counter: 1

Combining Assignment and Increment Operators

  • Increment can also be combined with assignment:

 
#include <iostream>
using namespace std;
int main() {
int a = 5;
a += 1; // same as a = a + 1
cout << a << endl; // 6
return 0;
}
  • This is functionally similar to a++ but more readable in some contexts.