6 Replies - 3654 Views - Last Post: 02 March 2009 - 04:24 PM Rate Topic: -----

#1 erewnoh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 01-March 09

Pre vs. Post Increment

Post icon  Posted 01 March 2009 - 09:04 PM

Hello all, my first official question post. Hopefully I'll keep these to a minimum.

I apologize if this is already covered. I did do a search but it didn't return anything, perhaps I didn't use it correctly. A google search returned some pretty good articles on what was more efficient (which appears that they are the same now with compiler optimization).

Anyway, on with it.

Is there ever a circumstance when using a pre vs. post increment operator could alter the number of iterations in a for, do, or do...while loop? What got me going was an example in a book I'm using to teach myself that said...
int a = 1;
int b = 1;
int c, d;

c = ++a; // a equals 2, c equals 2
d = b++; // b equals 2, d equals 1



Again, I apologize if it's covered here, and I apologize if this is trivial. Just one of those want to know things.

Edit - Aha, I found a case described where using the the increment operator in the conditional test has an impact.
int i = 0;

while (++i<10)
{
	/*statements*/
}


Though why you would do the incrementing inside the conditional test is beyond me. I'm of the opinion screen space is free, and another line won't hurt. Is there another reason I'm not seeing?

This post has been edited by erewnoh: 01 March 2009 - 09:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pre vs. Post Increment

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Pre vs. Post Increment

Posted 01 March 2009 - 09:28 PM

If it doesn't alter the situation then it is simply preference. Sometimes people use it inside subscripts:

//some loop
arr[i++];



This could lead to problems:

while (++i<10)
{
	/*statements*/
}



if one forgets that 'i' will increment before its iteration, so it would go from 1-9 rather then 0-9.
Was This Post Helpful? 0
  • +
  • -

#3 rs4  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 153
  • Joined: 01-February 09

Re: Pre vs. Post Increment

Posted 01 March 2009 - 09:31 PM

Its probly been so hard to find because there is no difference in speed, instead just when it runs, by putting it before(pre) the varible has one added to it first, were as putting it after the varible has one added to it at the end. As your program illistrates
Was This Post Helpful? 0
  • +
  • -

#4 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Pre vs. Post Increment

Posted 02 March 2009 - 07:38 AM


int a = 0;

if( ++a < 1){ ... } // add then execute

if( a++ < 2){ ... } // calculate the logical operation first




to design operators in OOP,

postfix ++ or -- are like if the compiler expects a Right Value,


a ++; // a ++ trivial;

++ a; // ++ a;

int operator++(int &a,int Trivial){ //post
	   Trivial = a;
		a=a+1;
		return Trivial; // a copy
}

int operator++(int &a){ //pre
	   a=a+1;
	   return a;
}



Was This Post Helpful? 0
  • +
  • -

#5 erewnoh  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 01-March 09

Re: Pre vs. Post Increment

Posted 02 March 2009 - 02:51 PM

Yes, what I was making sure of is that it doesn't affect the outcome of any loop unless used in the condition.

In Java I always use variable++, it's simply my preference, and there it won't alter the loop at all. Just affirming that this is the same in C++.
Was This Post Helpful? 0
  • +
  • -

#6 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Pre vs. Post Increment

Posted 02 March 2009 - 03:21 PM

Depending on what you're doing, when the variable is incremented is very important. For a simple example.

int a;

for(a=0;a<10;a++)
	/* Will run with a as 0 the first time */

for(a=0;a<10;++a)
	/* Will run with a as 1 the first time */

/* or */

int a=0;

printf("%d", a++);  /* Prints 0 */
printf("%d", ++a);  /* Prints 1 */



Sorry if this wasn't helpful or was more simple then what you were looking for, I'm just kind of new here and want to help
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Pre vs. Post Increment

Posted 02 March 2009 - 04:24 PM

View Postebadamageplan, on 2 Mar, 2009 - 03:21 PM, said:

for(a=0;a<10;++a)
	/* Will run with a as 1 the first time */




That is incorrect. Since the increment (regardless of post or pre) will only occur at the end of the iteration of that loop, a starts at 0 regardless of post or pre increment there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1