4 Replies - 27846 Views - Last Post: 25 October 2009 - 12:28 AM Rate Topic: -----

#1 Aklesh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-August 08

preincrement v/s Post increment inside for loop

Post icon  Posted 30 August 2008 - 09:02 AM

Hi all

Could you please let me know when can we use preincrement and post increment in for loop .


for (i=0;i<=4;++i);

and

for (i=0;i<=4;i++)


what is the basic difference between the two and when we use to what
Is This A Good Question/Topic? 0
  • +

Replies To: preincrement v/s Post increment inside for loop

#2 KYA  Icon User is offline

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

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: preincrement v/s Post increment inside for loop

Posted 30 August 2008 - 09:06 AM

My understanding (and I could be wrong, it has been known to happen) is in that scenario there is no difference. i will still increment before the next iteration of the for loop.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4405
  • View blog
  • Posts: 12,262
  • Joined: 18-April 07

Re: preincrement v/s Post increment inside for loop

Posted 30 August 2008 - 09:10 AM

In this situation neither matters because that portion of the for loop is incremented at the end of an iteration. Post and pre increment really only come into play when they are actually being used in a statement which uses the value. For example...


int i = 1;

// Post increment will add 1 AFTER the cout has been executed
cout << i++ << endl;

i = 1;

// Pre increment will add 1 BEFORE cout so that cout will use the new value
cout << ++i << endl;



The first statement will show "1" since it uses the value in the print and then increments it. The second will show "2" because it increments it and then prints.

Hope that helps. :)
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: preincrement v/s Post increment inside for loop

Posted 30 August 2008 - 05:58 PM

Well, it is generally thought that pre-increment is faster than post increment since post increment may involve a temporary variable. While there is some grain of truth to the matter it would really depend upon the compiler you were using.

So some people think that using preincrement in a for loop is more efficient:

for (i = 0; i < MAX; ++i) { ... }

Also if you want to write: i = i + 1; rather than i++ you should use ++i

This is of course assuming that the compiler creates a temporary value to store i in... since there is no NEED for it when i is not used in a larger expression -- so optimizing compilers don't waste their time.
Was This Post Helpful? 0
  • +
  • -

#5 ketki2k5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-October 09

Re: preincrement v/s Post increment inside for loop

Posted 25 October 2009 - 12:28 AM

take this as example


# include <stdlib.h>
# #include <stdio.h>
#
# int main(int argc, char **argp)
# {
# int x = 5;
#
# printf("x=%d\n", ++x);
# printf("x=%d\n", x++);
# printf("x=%d\n", x);
#
# return EXIT_SUCCESS;
# }

The output of this program is



1. x=6
2. x=6
3. x=7

In the first printf statement x is incremented before being passed to printf so the value 6 is output, in the second x is passed to printf (so 6 is output) and then incremented and the 3rd printf statement just shows that post increment following the previous statement by outputting x again which now has the value 7.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1