Pre vs. Post Increment

Page 1 of 1

6 Replies - 4667 Views - Last Post: 02 March 2009 - 04:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=89954&amp;s=5e97b4102e7378eb0f6203a4d4cb40d2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 erewnoh

Reputation: 18
• Posts: 78
• Joined: 01-March 09

Pre vs. Post Increment

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

• yay verily

Reputation: 3155
• Posts: 19,200
• 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++];

```

```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.

#3 rs4

Reputation: 29
• 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

#4 Plus

• D.I.C Regular

Reputation: 41
• 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;
}

```

#5 erewnoh

Reputation: 18
• Posts: 78
• 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++.

Reputation: 2
• 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

#7 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

Re: Pre vs. Post Increment

Posted 02 March 2009 - 04:24 PM

ebadamageplan, 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.