01 / 20
++ --

Increment & Decrement
Operators in C++

Master ++ and -- operators: understand the critical difference between prefix and postfix behavior and write cleaner, bug-free code.

C++ Operators

What Are Increment & Decrement Operators?

In C++, the increment operator (++) adds 1 to a variable, and the decrement operator (--) subtracts 1. They are shorthand for common operations you use in loops, counters, and pointer arithmetic.

\( \texttt{++x} \) or \( \texttt{x++} \)
Equivalent to \( x = x + 1 \)
\( \texttt{--x} \) or \( \texttt{x--} \)
Equivalent to \( x = x - 1 \)

Why Do They Matter?

1
Loops: Nearly every for loop uses \( \texttt{i++} \) or \( \texttt{++i} \) to advance the counter.
2
Conciseness: Writing \( \texttt{count++} \) is shorter and more idiomatic than \( \texttt{count = count + 1} \).
3
Pointer arithmetic: Iterating through arrays with \( \texttt{ptr++} \) is fundamental in C++.
4
Interviews & exams: Pre vs. post behavior is one of the most tested C++ concepts.

Two Forms: Prefix vs Postfix

Both ++ and -- come in two flavors. The difference is when the value changes relative to when it is used in an expression.

Prefix: \( \texttt{++x} \) / \( \texttt{--x} \)
First changes the value, then returns the new value.
Postfix: \( \texttt{x++} \) / \( \texttt{x--} \)
First returns the old value, then changes the variable.

The Core Rule

Prefix: \( \texttt{++x} \)
Step 1: \( x = x + 1 \)
Step 2: Return new \(x\)
Expression gets updated value
Postfix: \( \texttt{x++} \)
Step 1: Save old \(x\)
Step 2: \( x = x + 1 \)
Expression gets old value

Same Rules for Decrement

The -- operator follows the exact same prefix/postfix logic, but subtracts 1 instead of adding 1.

\( \texttt{--x} \) (Prefix)
Decrements x, then returns the new (smaller) value.
\( \texttt{x--} \) (Postfix)
Returns the old value, then decrements x.

Key Distinction: Standalone vs In Expressions

When used as a standalone statement, prefix and postfix are identical. The difference only matters when the operator is inside a larger expression.

// These two lines do exactly the same thing: ++x; // x goes up by 1 x++; // x goes up by 1 // But inside an expression, they differ: int a = ++x; // a gets the NEW value int b = x++; // b gets the OLD value

How to Apply: Step by Step

1
Identify the operator: Is it \( \texttt{++} \) or \( \texttt{--} \)? Is it prefix (before the variable) or postfix (after)?
2
Check context: Is the operator standalone (e.g., x++;) or inside an expression (e.g., y = x++)?
3
If standalone: Simply add or subtract 1. Prefix and postfix behave the same.
4
If in an expression: For prefix, change first, then evaluate. For postfix, evaluate with the old value, then change.
5
Track the variable: After the line executes, the variable is always updated regardless of prefix or postfix.

Example 1: Prefix Increment

int x = 3; int y = ++x; // prefix: increment first cout << x; // Output: 4 cout << y; // Output: 4
x (before) 3 → x (after ++) 4 → y = new value 4 Start Increment first Then assign

Example 2: Postfix Increment

int x = 3; int y = x++; // postfix: use first, then increment cout << x; // Output: 4 cout << y; // Output: 3
x (save old) 3 → y = old value 3 → x (after ++) 4 Save old value Assign old to y Then increment x

Example 3: Combined in a Loop

for (int i = 0; i < 3; i++) { cout << i << " "; } // Output: 0 1 2

In a for loop's update expression, \( \texttt{i++} \) and \( \texttt{++i} \) produce the same loop behavior because the result of the expression is discarded: only the side effect (incrementing i) matters.

Example 4: Tricky Multi-Use

int a = 5; int b = a++ + ++a; // Both a and b have UNDEFINED values!

Golden rule: Never use ++ or -- on the same variable more than once in a single statement. Split it into separate lines instead.

Example 5: Decrement in Action

int count = 10; while (--count > 7) { cout << count << " "; } // Output: 9 8
1
Iteration 1: \( \texttt{--count} \) decrements 10 to 9, then checks \( 9 > 7 \): true. Prints 9.
2
Iteration 2: \( \texttt{--count} \) decrements 9 to 8, then checks \( 8 > 7 \): true. Prints 8.
3
Iteration 3: \( \texttt{--count} \) decrements 8 to 7, then checks \( 7 > 7 \): false. Loop ends.

Common Mistakes

✕ Double Modification
Using \( \texttt{++x} \) and \( \texttt{x++} \) on the same variable in one statement causes undefined behavior.
✕ Confusing Return Value
Assuming \( \texttt{x++} \) returns the new value. It returns the old value: the increment happens after.
✕ On Non-Lvalues
Trying \( \texttt{5++} \) or \( \texttt{++(x+1)} \): you can only increment/decrement a variable, not a literal or expression.

Summary Comparison Table

Prefix (++x / --x) Postfix (x++ / x--)
Order of Operations Change first, then use Use first, then change
Expression Value New value Old value
Variable After Incremented / decremented Incremented / decremented
Standalone Use Identical result Identical result
Performance No temp copy needed May create temp copy
Recommended for Loops ✓ Preferred Works but less idiomatic

Question 1 of 5

If \( \texttt{int x = 10;} \) and \( \texttt{int y = ++x;} \), what are the values of \(x\) and \(y\)?

Question 2 of 5

If \( \texttt{int x = 10;} \) and \( \texttt{int y = x++;} \), what are the values of \(x\) and \(y\)?

Question 3 of 5

Given \( \texttt{int n = 5;} \), what does \( \texttt{--n} \) evaluate to, and what is \(n\) afterward?

Question 4 of 5

In a for loop, does it matter whether you write \( \texttt{i++} \) or \( \texttt{++i} \) as the update expression?

Question 5 of 5

What is wrong with this code?
\( \texttt{int x = 3; int y = x++ + ++x;} \)