# Need a bit of help understaning Postfix and Prefix

Page 1 of 1

## 7 Replies - 1438 Views - Last Post: 20 October 2011 - 07:54 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=251777&amp;s=a0f997c6a256a89cadedc382325002cf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rvbsanjose

Reputation: 0
• Posts: 26
• Joined: 17-October 11

# Need a bit of help understaning Postfix and Prefix

Posted 17 October 2011 - 11:46 PM

I'm taking a Intro to Computer Science and Intro to C programming class at De Anza and struggling a bit understanding the difference between postfix and prefix increment/decrement expressions. The examples I'm following along with in the book aren't doing it for me so I hope someone here can word it in a way that will get me to understand it. Much appreciated.
Is This A Good Question/Topic? 0

## Replies To: Need a bit of help understaning Postfix and Prefix

### #2 andy_pleasants

Reputation: 41
• Posts: 122
• Joined: 08-July 10

## Re: Need a bit of help understaning Postfix and Prefix

Posted 18 October 2011 - 01:47 AM

It's all about when the expression is actually evaluated (or when you get the "answer" to the expression), so say x = 0 if you do y = x++ this is equivalent to:

```y = x
x = x + 1

```

So y is set to the value of x and then x is incremented, this is postincrement (increment AFTER evaluation).

If you were to do y = ++x this is equivalent to:

```x = x + 1
y = x

```

Notice how the increment is now before the evaluation. x++, ++x is just short hand basically, and the same rules apply for pre and postdecrement

This post has been edited by andy_pleasants: 18 October 2011 - 01:48 AM

### #3 ajwsurfer

• D.I.C Regular

Reputation: 21
• Posts: 376
• Joined: 24-October 06

## Re: Need a bit of help understaning Postfix and Prefix

Posted 18 October 2011 - 07:27 AM

```
#include <iostream>
using namespace std;

int main()
{
int x = 2;
int y = 2;

cout << x++ << endl; // x = 2
cout << x << endl;   // x = 3

cout << ++y << endl; // y = 3
cout << y << endl;   // y = 3

// also works with x-- and y--

}

```

### #4 rvbsanjose

Reputation: 0
• Posts: 26
• Joined: 17-October 11

## Re: Need a bit of help understaning Postfix and Prefix

Posted 18 October 2011 - 10:35 AM

andy_pleasants, on 18 October 2011 - 01:47 AM, said:

It's all about when the expression is actually evaluated (or when you get the "answer" to the expression), so say x = 0 if you do y = x++ this is equivalent to:

```y = x
x = x + 1

```

So y is set to the value of x and then x is incremented, this is postincrement (increment AFTER evaluation).

If you were to do y = ++x this is equivalent to:

```x = x + 1
y = x

```

Notice how the increment is now before the evaluation. x++, ++x is just short hand basically, and the same rules apply for pre and postdecrement

Thx for the explanation Andy. After some visuals, I do understand it now. I do have one last question though...

If postfix and prefix evaluate to the same value, in which cases would someone use postfix as oppose to prefix?

### #5 jimblumberg

Reputation: 5487
• Posts: 17,064
• Joined: 25-December 09

## Re: Need a bit of help understaning Postfix and Prefix

Posted 18 October 2011 - 11:40 AM

You should normally use the pre-increment (++i) to avoid the possibility of a temporary copy being made. But there are times where you will need to use the post-increment, to insure the value is altered after it is used.

Jim

### #6 andy_pleasants

Reputation: 41
• Posts: 122
• Joined: 08-July 10

## Re: Need a bit of help understaning Postfix and Prefix

Posted 19 October 2011 - 03:28 AM

Quote

If postfix and prefix evaluate to the same value, in which cases would someone use postfix as oppose to prefix?

It's more about when the increment happens pre-increment is saying "add 1 to the value, then give me the value" and post-increment is asking "give the value, then add 1 to it".

And also, technically they don't evaluate to the same value x++ evaluates to x and ++x evaluates to x + 1

### #7 rvbsanjose

Reputation: 0
• Posts: 26
• Joined: 17-October 11

## Re: Need a bit of help understaning Postfix and Prefix

Posted 19 October 2011 - 09:14 AM

I would like to thank Andy and everyone else who left a comment. I thought I was going to be lost but know feel kind of dumb for not getting the material in the first place. I'm sure I'll be asking other questions and from what I read online, I am sure I would be asking questions again about pointers next quarter. Thanks again!!!

### #8 andy_pleasants

Reputation: 41
• Posts: 122
• Joined: 08-July 10

## Re: Need a bit of help understaning Postfix and Prefix

Posted 20 October 2011 - 07:54 AM

Quote

I thought I was going to be lost but know feel kind of dumb for not getting the material in the first place

The only dumb questions are the ones that aren't asked! Keep asking