Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,358 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,170 people online right now. Registration is fast and FREE... Join Now!




preincrement v/s Post increment inside for loop

 
Reply to this topicStart new topic

preincrement v/s Post increment inside for loop

Aklesh
post 30 Aug, 2008 - 08:02 AM
Post #1


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 1

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
User is offlineProfile CardPM

Go to the top of the page

KYA
post 30 Aug, 2008 - 08:06 AM
Post #2


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,205



Thanked 50 times

Dream Kudos: 1150
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 30 Aug, 2008 - 08:10 AM
Post #3


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


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

cpp


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. smile.gif
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 30 Aug, 2008 - 04:58 PM
Post #4


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 times

Dream Kudos: 550
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 04:20AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month