7 ms·
I have no idea why anyone would use ++i (though technically correct) instead of i++ in a for loop's incrementation clause since the incrementation is done after
by Hexstream 17y ago
I have no idea why anyone would use ++i (though technically correct) instead of i++ in a for loop's incrementation clause since the incrementation is done after the loop body, not before... That just seems like willfully confusing intuition.
- shrughes 17y agoIf you were confused in such a way you'd end up thinking that i would be incremented with "i++" before the loop body ran. For "++i" to be confusing would be seriously messed up.
- mgreenbe 17y agoI think using ++i instead of i++ everywhere is clearer, actually -- there's no need to think about temporaries, and the order of evaluation is (marginally) more transparent. Not that pre- and post-increment are ever that intuitive or transparent...
- lallysingh 17y agolook at for loops like this: for (FOO; BAR; BAZ) { BODY } as: FOO; while (BAR) { BODY; BAZ; } Hence, ++i and i++ mean the same in this context. The difference, as mentioned already, is that i++ creates a copy of i.
- silentOpen 17y agoUh? Maybe with a brain-damaged compiler?
- shrughes 17y agoOr with C++. There's no reason to adopt i++ sometimes and ++i other times depending on what the type is.
- silentOpen 17y agoC++ copies values differently (or at all) for different types under prefix increment vs. postfix increment? Seriously?
- shrughes 17y agoYes. When overloading prefix and postfix increment, there are two functions to write, and the postfix version returns a copy of what you've incremented. It's impossible for a compiler to optimize this away in the general situation.
- dkarl 17y agoThere's no difference between C and C++, except that in C++, you can define prefix and postfix operators on user-defined types. The two are defined separately because C++ was created for people who are really finicky about performance. (For some reason some people are really bothered by the possibility that you could provide really insane definitions, as if it wasn't already possible to create insanely named functions and methods....) Anyway, even in C, ++i and i++ change the value of i and evaluate to a value. So there are two values: the new value of i, and the value of the expression. In the case of prefix ++, the two values are the same. In the case of postfix ++, the two values are different. If you ignore the value of the expression, then ++i and i++ can be used interchangeably. The compiler will probably figure this out and produce the same code.
- amalcon 17y agoUsing i++ was the recommended practice before C++ came along and let people change what i++ means. That convention was even the reason it's C++ and not ++C. In C++, it's suggested to do ++i because it will sometimes result in fewer calculations if i is an object with overloaded increment operators.
- dkarl 17y agoIs this true? It was never explained to me this way, and it doesn't make much sense. The increment will always be performed unless it can be optimized away. In places where the two are equivalent, using i++ might impose a space penalty if you have to store both values, which can be a significant consideration for large objects. I don't know what you mean by "change what i++ means" since you can't redefine ++ for any types for which it has a meaning in C. (Or maybe you can, but I wouldn't know because it would never done in sanely written code; the possibility definitely isn't taken into account in any C++ coding recommendations I've read.) Mainly I was taught that situations that require i++ are less common, more subtle, and easier to get wrong, so programmers should make them stand out by using ++i everywhere else.
- avigesaa 17y agoI use ++i in for loops because pre-incrementing is the semantically correct option. I've never understood why i++ came into favor; can anyone shine some light on it for me?
- lincolnq 17y agoMy hypothesis: it has better rhythm -- the repetition of 'i' at the beginning of each piece of the for loop makes it nice and easy to remember. I bet people would tend to learn this when they were first learning how to code and then never change because it never caused them any problems.