Loading...
August 18, 2025

Post-Increment vs. Pre-Increment: A Lesson Across Languages

A coding quirk I discovered and later found in multiple language

About a year ago, I ran into something that did not behave the way I expected. I shared it with my team at the time, but I have always thought it was worth writing down more broadly. It started as a surprise in my own coding, and after looking into other languages, I realized the same behavior holds true in many of them.

Incrementing a variable seems simple enough, but whether you write count++ or ++count can change the result in ways that are easy to overlook.

The Discovery

Here is what I first noticed while coding:

count = 0;
writeOutput(count++ & '<br>');
writeOutput(count & '<br>');

Output:

0
1

The first line prints 0, not 1, because post-increment uses the current value first and only increments after.

If I change it to pre-increment:

count = 0;
writeOutput(++count & '<br>');

Output:

1

The Rule

  • Post-increment (count++): use the current value, then increment afterward.
  • Pre-increment (++count): increment first, then use the updated value.

Examples in Other Languages

JavaScript
let count = 0;
console.log(count++); // 0
console.log(count);   // 1

count = 0;
console.log(++count); // 1
Java
int count = 0;
System.out.println(count++); // 0
System.out.println(count);   // 1

count = 0;
System.out.println(++count); // 1
C#

int count = 0;
Console.WriteLine(count++); // 0
Console.WriteLine(count);   // 1

count = 0;
Console.WriteLine(++count); // 1

Languages That Handle This Differently

Some modern languages avoid this distinction altogether:

  • Python: no ++ operator, use count += 1 instead.
  • Ruby: no ++ operator.
  • Swift / Kotlin: dropped ++ to reduce confusion.
  • Go: supports ++ but only as a statement, not as an expression.

Takeaway

This behavior isn’t unique to one language. It comes from a long lineage of C-style syntax. Post-increment and pre-increment are simple concepts, but they can produce surprising results if you don’t remember when the increment actually happens.

ColdFusion developers may remember that Ben Nadel wrote about this all the way back in 2007: Learning ColdFusion 8: All Hail the New Operator.

Even after years of coding, it’s details like these that remind us how much there is always left to learn.

Share Article