question about for loop

Started by
6 comments, last by CipherCraft 18 years, 1 month ago
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 ^^
Advertisement
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. :)
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
Quote:Original post by CipherCraft
++i adds one to i, period.

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

Aah, thank you a lot ^^
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.

Stephen M. Webb
Professional Free Software Developer

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

This topic is closed to new replies.

Advertisement