5 Replies - 7076 Views - Last Post: 14 May 2011 - 08:29 AM Rate Topic: -----

#1 aine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-May 11

Prefix vs. Postfix Operators in C++

Posted 13 May 2011 - 07:20 PM

i wanna know when j++ and ++j behave like the same and when they behave differently

This post has been edited by macosxnerd101: 13 May 2011 - 07:41 PM
Reason for edit:: Title renamed to be more descriptive

Is This A Good Question/Topic? 0
  • +

Replies To: Prefix vs. Postfix Operators in C++

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Prefix vs. Postfix Operators in C++

Posted 13 May 2011 - 07:43 PM

Moved to C/C++.

The prefix ++j operator has a higher precedence than the postfix operator j++. For example:
int j = 0;
arrayName[++j] = 1; //arrayName[1] = 1
arrayName[j++] = 2; //arrayName[1] = 2, j = 2


Was This Post Helpful? 1
  • +
  • -

#3 RevTorA  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 76
  • View blog
  • Posts: 246
  • Joined: 22-April 11

Re: Prefix vs. Postfix Operators in C++

Posted 13 May 2011 - 08:05 PM

The way I've always thought about it is that the operator position shows you the order that things happen. If the operator is before the operand (++j), then the operand is incremented BEFORE it is returned, whereas if the operator is after the operand then the operand is returned and THEN incremented.

int i = 0;
int j = 1;

i = j++;
//Now i will be 1, since j is returned and then incremented. j = 2

i = ++j;
//Now i will be 3, since j is incremented, and then returned.


Was This Post Helpful? 2
  • +
  • -

#4 aine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-May 11

Re: Prefix vs. Postfix Operators in C++

Posted 14 May 2011 - 06:20 AM

i want to know when it behave like the same and when they behave differently
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Prefix vs. Postfix Operators in C++

Posted 14 May 2011 - 06:31 AM

Please see the following links for descriptions about the differences between prefix and postfix expressions.

Prefix expression from this link.

Quote

Both the prefix and postfix increment and decrement operators affect their operands. The key difference between them is when the increment or decrement takes place in the evaluation of an expression. (For more information, see Postfix Increment and Decrement Operators.) In the prefix form, the increment or decrement takes place before the value is used in expression evaluation, so the value of the expression is different from the value of the operand. In the postfix form, the increment or decrement takes place after the value is used in expression evaluation, so the value of the expression is the same as the value of the operand. For example, the following program prints "++i = 6":


Postfix expression from this link:

Quote

It is important to note that a postfix increment or decrement expression evaluates to the value of the expression prior to application of the respective operator. The increment or decrement operation occurs after the operand is evaluated. This issue arises only when the postfix increment or decrement operation occurs in the context of a larger expression.


Jim
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Prefix vs. Postfix Operators in C++

Posted 14 May 2011 - 08:29 AM

They never really "act the same" although from your perspective sometimes the results are identical but there is always a technical difference.

The technical difference is this: When the compiler sees ++i it will take the value of i and increment it by 1, then use the new value in the current expression. When it sees j++ it will save the current value of j in a temporary, then increment j, and use the saved OLD value of j for the current expression.

So for example if you have:

for(int i = 0; i < 10; i++) { } -- Technically what is happening is that a temporary value is being created/destroyed each time i++ occurs. From your perspective there is no difference as this temporary value of i is not being used (so many compilers actually optimize it away) but technically it should semantically still be created on each iteration.

now: for(int i = 0; i < 10; ++i) { } -- functions exactly the same, the SAME values of i are available inside the loop in exactly the same order (something I used to have difficulty understanding for some reason).

So why don't we always use the second version, shouldn't it be faster? Well yes, technically it should, and in fact there are times when the ++i is better (Specifically when using classes that have the increment operator's overloaded). However with ints/floats/pointers the compiler generally optimizes out the creation of the temp and so both loops function exactly the same although there is a semantic difference and TECHNICALLY there is a difference - but the reality is that they function the same.

So remeber - post increment creates a temp and increments it and then uses the current value. And pre-increment increments the current value and then uses it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1