@brouhaha @mtomczak@qoto.org @b0rk i believe it's my previous toot with the for loop.
I believe the i++ statement in the for loop init is a tradition. Any loop update statement can be used here, loops dont have to just increment.
The for loop is basically syntactic sugar for a while construct
for(a;b;c) <statement> is equivalent to a;while(b) {<statement>; c}
@ives @f4grx @b0rk I used a lot of old C++ compilers (back to 1982), and was never aware of that. I could see it being different in a subexpression, but it slightly surprises me that it would ever result in different code when it's an entire expression. Even if a compiler was inclined to generate different code, in the case where it's not a subexpression I'd expect even the most rudimentary peephole optimizer to eliminate the difference.
@brouhaha @f4grx @b0rk Definitely one of the more trivial situations to optimize. I suppose it could get a bit more complex in expressions or when overriding operators.
And then there are processors like the Motorola 68000 that have support for post-increment and pre-decrement, but not the others.
But yeah, best to write code that expresses your intention as clearly as possible and let the compiler deal with optimizing it.
@sethrichards @f4grx @b0rk I disagree. When the increment is a standalone expression, not a subexpression, they are 100% identical.
for (i= 0; i < limit; i++)
has exactly the same semantics as
for (i= 0; i < limit; ++i)
which is the case I was questioning.
Your statement is correct when the increment is a subexpression, so a[i++] is very different than a[++i].
@mtomczak @f4grx @b0rk In that kind of loop, is there any benefit to brace initialization of a scalar variable, or preincrement rather than postincrememt? Or are those preferred for some broad reason?