Skip to content
C++ Better Explained
Go back
C++ Increment and Decrement Operators: ++i vs i++ Explained

C++ Increment and Decrement Operators

The ++ and -- operators are everywhere in C++ — especially in loops. They look simple, but the difference between ++i and i++ trips up almost every beginner. Let’s make it crystal clear.


What ++ and — Actually Do

The increment operator ++ adds 1 to a variable. The decrement operator -- subtracts 1. They’re just shorthand:

#include <iostream>

int main() {
    int score = 10;
    score++;              // same as score = score + 1
    std::cout << score << "\n";  // 11

    int lives = 3;
    lives--;             // same as lives = lives - 1
    std::cout << lives << "\n";   // 2
    return 0;
}

So far so good. The interesting part is that each operator has two forms: one where the symbol comes before the variable, and one where it comes after.


Pre-Increment vs Post-Increment: The Real Difference

Both ++i and i++ change i by the same amount. The difference is the value the expression produces while doing it:

You only notice this when you use the result of the expression in the same line:

#include <iostream>

int main() {
    int a = 5;
    int b = a++;   // b gets the OLD value (5), then a becomes 6
    std::cout << "a = " << a << ", b = " << b << "\n";  // a = 6, b = 5

    int c = 5;
    int d = ++c;   // c becomes 6 FIRST, then d gets 6
    std::cout << "c = " << c << ", d = " << d << "\n";  // c = 6, d = 6
    return 0;
}

Read a++ as “give me a, then increment.” Read ++a as “increment, then give me a.” That one sentence is the whole concept.


Which Should You Use in a for Loop?

Here’s the good news: in an ordinary counting loop, it makes no difference at all.

for (int i = 0; i < 5; i++)   // works
for (int i = 0; i < 5; ++i)   // identical result

The loop ignores the value the expression produces — it only cares about the side effect of adding 1. For a plain int, compilers generate the exact same machine code either way, so there’s no performance cost.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

So why do experienced programmers prefer ++i? Because of iterators and objects, covered next.


Why Pros Prefer ++i

When you loop over a container with an iterator (or any custom object), post-increment has to make a copy of the old value to return it, then increment the original. Pre-increment skips the copy entirely:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> nums = {10, 20, 30};
    for (auto it = nums.begin(); it != nums.end(); ++it) {
        std::cout << *it << "\n";
    }
    return 0;
}

For a heavyweight object, that saved copy can actually matter. Since ++it is never slower and sometimes faster, ++i is the safe default habit — even on plain integers where it doesn’t matter.


A Gotcha: Don’t Increment the Same Variable Twice

Avoid writing something clever like i = i++; or arr[i] = i++;. Modifying the same variable more than once in a single statement leads to confusing, unreliable results. Keep each statement doing one clear thing:

// Unclear and error-prone:
// i = i++;

// Clear:
i++;

If a line is hard to read, split it up. Readable code beats clever code every time.


The Decrement Operators Work the Same Way

Everything above applies to --, just in the opposite direction. --i subtracts 1 then returns the new value; i-- returns the old value then subtracts 1. Countdown loops use it constantly:

#include <iostream>

int main() {
    for (int i = 5; i > 0; --i) {
        std::cout << i << " ";
    }
    std::cout << "Liftoff!\n";   // 5 4 3 2 1 Liftoff!
    return 0;
}

Quick Reference

ExpressionValue it producesEffect on i
++inew value (after +1)i increases by 1
i++old value (before +1)i increases by 1
--inew value (after -1)i decreases by 1
i--old value (before -1)i decreases by 1


Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
Find the Maximum and Minimum in C++ (max, min, max_element)
Next Post
Integer Division in C++: Why 5 / 2 Equals 2 (Not 2.5)

Keep Learning