Sign in to follow this  

question about for loop

This topic is 4306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, It's probably stupid, but I want to be sure : what's the difference between for (int i = 0; i < 100; i++) and for (int i = 0; i < 100; ++i) ?? I thought that ++i or i++ (in this particular case) would be exactly the same. But I don't remember where, but I think I've read something about a difference between both. And during a debugging, I've noticed that in the std::ofstream implementation, they always use the ++i notation. So, am I right in thinking that it's exactly the same and it's just to one's liking ? Or is there a slight difference (maybe a few assembly instructions that vary between both ?) ?? And sorry if this sound stupid, I sometime have weird questions that bother me until I've got an answer ^^

Share this post


Link to post
Share on other sites
In this particular case, they are exactly the same. Whether you use the prefix or postfix increment, the loop will still start at zero and end at 100, and i will be incremented after the loop has finished one cycle.

People like to use the prefix increment (++i) version because they think it's faster. Technically, it is, as the postfix increment (i++) actually returns the value of i before the increment, so it has to store it in a temporary variable (or register, whatever). But if you're not using it, the compiler will just optimize it out anyway.

Personally, I just use the postfix increment because it looks nicer. :)

Share this post


Link to post
Share on other sites
Hi,

++i adds one to i, period.
i++ adds one to i and returns a copy of its previous value.

The STL uses the latter because it uses iterators alot. These are more than POD's and can therefore be expensive to copy. And since you do nothing with the returned copy - quite possibly inside a tight loop - it's overhead that you want to eliminate.

Rule of thumb: use ++i, useless you really need the previous value.

hth,
CipherCraft

Share this post


Link to post
Share on other sites
Quote:
Original post by bpoint
People like to use the prefix increment (++i) version because they think it's faster. Technically, it is, as the postfix increment (i++) actually returns the value of i before the increment, so it has to store it in a temporary variable (or register, whatever). But if you're not using it, the compiler will just optimize it out anyway.


The compiler is not allowed to elide the constructor of the temp object created by the postfix increment operator, nor is it allowed to optimize away its destructor. If the iterator you're incrememting has non-trivial construction semantics (such as, for example, a std::deque::iterator or a std::map::iterator), you're paying a potentially high and unnecessary cost for something of dubious aesthetic value.

Prefer the preincrement operator (++i) if you don't need the value of the expression. Just like keeping the constant on the left of a comparison operation, if you learn to see it as a more visually pleasing way of expressing yourself, you will come out ahead.

Share this post


Link to post
Share on other sites
Quote:
Original post by BregmaThe compiler is not allowed to elide the constructor of the temp object created by the postfix increment operator, nor is it allowed to optimize away its destructor. If the iterator you're incrememting has non-trivial construction semantics (such as, for example, a std::deque::iterator or a std::map::iterator), you're paying a potentially high and unnecessary cost for something of dubious aesthetic value.

Yes, but as you mentioned, only if you're using the postfix on a variable that has an overridden operator (ie: STL) does the compiler has to do something with it.

For ints (as the OP questioned), there is no temporary object creation or destruction.

Quote:
Just like keeping the constant on the left of a comparison operation

I personally dislike reading code that has the constant on the left. The logic doesn't flow correctly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
Quote:
Original post by CipherCraft
++i adds one to i, period.

++i adds one to i and returns the new value.


Correct, just like all expressions in C++ return their value, but there's no penalty if you do not use them.

I'll quote Stroustrup on this one, with one of his transcribed expressions:
Quote:
Stroustrup, 6.2.5 Increment and Decrement
y = ++x equals y = (x += 1)

y = x++ equals y = (t = x, x += 1, t)



Main issue is, that no copy is being made, so other than the intended operation - adding 1 - no penalties will be suffered.

cu,
CipherCraft

Share this post


Link to post
Share on other sites

This topic is 4306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this