7 Replies - 1002 Views - Last Post: 20 October 2011 - 07:54 AM Rate Topic: -----

#1 rvbsanjose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • 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

Was This Post Helpful? 2
  • +
  • -

#3 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • 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--

}


Was This Post Helpful? 2
  • +
  • -

#4 rvbsanjose  Icon User is offline

  • New D.I.C Head

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

Re: Need a bit of help understaning Postfix and Prefix

Posted 18 October 2011 - 10:35 AM

View Postandy_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?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • 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
Was This Post Helpful? 1
  • +
  • -

#6 andy_pleasants  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#7 rvbsanjose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!!!
Was This Post Helpful? 0
  • +
  • -

#8 andy_pleasants  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1