Lesson 19 of 60 – Increment and Decrement Operators in C++
32%

Increment and Decrement Operators in C++

Increment and decrement operators are used to increase or decrease the value of a variable by 1. C++ provides two operators: ++ for increment and -- for decrement.

Note: The ++ operator increases a value by 1, while the -- operator decreases a value by 1.

1. What are Increment and Decrement Operators?

Increment and decrement operators are special unary operators that change the value of a variable by exactly one.

int number = 10;

number++;

After this statement, number becomes 11.

2. Increment Operator ++

The ++ operator increases the value of a variable by 1.

int count = 5;

count++;

The new value of count is 6.

3. Increment Using ++ Before the Variable

When ++ is written before the variable, it is called pre-increment.

int number = 10;

++number;

The value becomes 11.

4. Increment Using ++ After the Variable

When ++ is written after the variable, it is called post-increment.

int number = 10;

number++;

The value becomes 11.

5. Decrement Operator --

The -- operator decreases the value of a variable by 1.

int count = 10;

count--;

The new value of count is 9.

6. Decrement Using -- Before the Variable

When -- is written before the variable, it is called pre-decrement.

int number = 10;

--number;

The value becomes 9.

7. Decrement Using -- After the Variable

When -- is written after the variable, it is called post-decrement.

int number = 10;

number--;

The value becomes 9.

8. Pre-Increment Operator

In pre-increment, the variable is increased first and then its new value is used in the expression.

int x = 5;
int y = ++x;

After execution:

  • x = 6
  • y = 6

9. Post-Increment Operator

In post-increment, the current value is used first and then the variable is increased.

int x = 5;
int y = x++;

After execution:

  • x = 6
  • y = 5

10. Pre-Decrement Operator

In pre-decrement, the variable is decreased first and then its new value is used.

int x = 5;
int y = --x;

After execution:

  • x = 4
  • y = 4

11. Post-Decrement Operator

In post-decrement, the current value is used first and then the variable is decreased.

int x = 5;
int y = x--;

After execution:

  • x = 4
  • y = 5

12. Pre-Increment vs Post-Increment

Operator Example Value Used Variable After Operation
Pre-increment ++x New value Increased by 1
Post-increment x++ Old value Increased by 1

13. Pre-Decrement vs Post-Decrement

Operator Example Value Used Variable After Operation
Pre-decrement --x New value Decreased by 1
Post-decrement x-- Old value Decreased by 1

14. Increment in a Simple Program

#include <iostream>

int main() {

    int count = 1;

    count++;
    count++;
    count++;

    std::cout << count;

    return 0;
}

The value of count becomes 4.

15. Decrement in a Simple Program

#include <iostream>

int main() {

    int count = 5;

    count--;
    count--;

    std::cout << count;

    return 0;
}

The final value of count is 3.

16. Increment and Decrement in Expressions

Increment and decrement operators can be used as part of expressions, but their position determines whether the old or new value is used.

int x = 5;

int a = ++x;
int b = x++;

After the first statement, x is 6 and a is 6. After the second statement, b is 6 and x becomes 7.

17. Increment in a for Loop

The increment operator is frequently used to increase a loop counter.

for (int i = 1; i <= 5; i++) {
    std::cout << i << std::endl;
}

Here, i++ increases the value of i after each loop iteration.

18. Decrement in a for Loop

The decrement operator can be used to count backward.

for (int i = 5; i >= 1; i--) {
    std::cout << i << std::endl;
}

Here, i-- decreases the value of i after each iteration.

19. Increment with User Input

#include <iostream>

int main() {

    int number;

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

    number++;

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

    return 0;
}

20. Decrement with User Input

#include <iostream>

int main() {

    int number;

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

    number--;

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

    return 0;
}

21. Incrementing a Counter

Counters are often increased using the increment operator.

int counter = 0;

counter++;
counter++;
counter++;

std::cout << counter;

The final value is 3.

22. Decrementing a Counter

A counter can also be decreased using the decrement operator.

int counter = 5;

counter--;
counter--;

std::cout << counter;

The final value is 3.

23. Using Increment with Conditions

int number = 1;

if (number < 5) {
    number++;
}

std::cout << number;

The value becomes 2 because the condition is true.

24. Using Decrement with Conditions

int number = 5;

if (number > 0) {
    number--;
}

std::cout << number;

The value becomes 4.

25. Incrementing Multiple Variables

int a = 10;
int b = 20;

a++;
b++;

std::cout << a << std::endl;
std::cout << b;

Both variables are increased by 1.

26. Increment and Decrement with char

Increment and decrement operators can also be applied to integral types such as char.

char letter = 'A';

letter++;

std::cout << letter;

For the ordinary execution character set, this commonly results in the next character such as B.

27. Important Difference Between Pre and Post Operators

int x = 10;

int a = ++x;
int b = x++;

Step by step:

  • ++x first changes x from 10 to 11, then assigns 11 to a.
  • x++ assigns the current value 11 to b, then changes x to 12.

Therefore:

  • a = 11
  • b = 11
  • x = 12

28. Complete Increment and Decrement Example

#include <iostream>

int main() {

    int number = 10;

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

    number++;

    std::cout << "After increment: "
              << number << std::endl;

    number--;

    std::cout << "After decrement: "
              << number << std::endl;

    int result = ++number;

    std::cout << "Pre-increment result: "
              << result << std::endl;

    result = number--;

    std::cout << "Post-decrement result: "
              << result << std::endl;

    return 0;
}

29. Common Increment and Decrement Mistakes

  • Confusing ++x with x++ when used inside an expression.
  • Confusing --x with x--.
  • Forgetting that ++ increases a value by exactly 1.
  • Forgetting that -- decreases a value by exactly 1.
  • Using these operators with expressions that are not modifiable variables.
  • Writing complicated expressions where multiple changes to the same variable make the result difficult to understand.

30. Increment and Decrement – Final Summary

Operator Name Example Effect
++ Increment x++ Increases x by 1
-- Decrement x-- Decreases x by 1
++x Pre-increment y = ++x Increases x first, then uses new value
x++ Post-increment y = x++ Uses old value, then increases x
--x Pre-decrement y = --x Decreases x first, then uses new value
x-- Post-decrement y = x-- Uses old value, then decreases x

📌 Key Points

  • The ++ operator increases a variable by 1.
  • The -- operator decreases a variable by 1.
  • ++x is called pre-increment.
  • x++ is called post-increment.
  • --x is called pre-decrement.
  • x-- is called post-decrement.
  • Pre-increment uses the new value in an expression.
  • Post-increment uses the old value before increasing the variable.
  • Pre-decrement uses the new value after decreasing the variable.
  • Post-decrement uses the old value before decreasing the variable.
  • Increment and decrement operators are commonly used with loops and counters.

🧠 Quick Quiz

Question: What is the value of y after the following code?

int x = 5;
int y = ++x;