You are here:



What is the difference between


Can you please give me an example and tell me how each produces a different output?

Hi, Mandy.

These operators are referred to as postfix and prefix operators, respectively.  There are many such operators, and these in particular are the increment prefix and postfix operators.

A prefix operator executes before the rest of the code on the line.  When using a prefix operator, whatever operation it causes will execute immediately when the line is run, then the rest of the code on the line will execute.  The prefix operator directly changes the variable it operates on, so the rest of the line will use the modified version of the variable.

The postfix operator executes after the rest of the code on the line.  When using a postfix operator, all of the code on the line will execute, then the operation caused by the postfix operator will be executed.  The postfix operator directly changes the variable it operates on, as well.

Take the following code as an example:

   int i = 5;        // line 1
   int j = ++i * 2;  // line 2
   int k = i-- * 2;  // line 3

The final value for i in the above code is 5.  The values for j and k are both 12.  In line 2, the variable i is incremented before anything else on the line occurs.  An equivalent to line 2 would be:

   i = i + 1;
   int j = i * 2;

In line 3, the variable i is decremented after everything else on the line occurs.  An equivalent to line 3 would be:

   int k = i * 2;
   i = i - 1;

So you can see that during the execution of the code, the value of i changes twice, once for each of the postfix and prefix operators.  The j and k variables end up being the same, even though, at a quick glance, you might not expect them to.  The reason they are the same is due to when the change in i occurs -- before the multiplication in line 2 and after the multiplication in line 3.

If you're an optimization geek, it's useful to know that the prefix operator is usually faster than the postfix operator, so in cases where it doesn't matter which one you use (such as inside your typical for loop), it's best to go with the prefix operator.  I can go into why the prefix operator is faster, if you like, but it's good enough just to know that it is slightly faster than the postfix operator.

If you have any questions about anything I've written, or if I did not fully answer your question, or even if you just have other questions, feel free to let me know!


All Answers

Answers by Expert:

Ask Experts


Joseph Moore


I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.


I've been writing C++ code for 12 years, both on my own in my spare time and professionally.


Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2017 All rights reserved.

[an error occurred while processing this directive]