4 ms·
> no for loops Oh wow, I've read bits and pieces about early iterations of C but I didn't realise for loops weren't there from the start.
by jamesfinlayson 5y ago
> no for loops
Oh wow, I've read bits and pieces about early iterations of C but I didn't realise for loops weren't there from the start.
- edgyquant 5y agoFor loops are syntactic sugar for while loops, e.g. for(int i = 0; i < 10; i++); Is just int i = 0; while(i < 10) i++;
- kbelder 5y agoHonestly, I think that's cleaner.
- legalcorrection 5y agoThe problem is that the i++ goes at the end of the loop body. When the loop body is long, the increment can be many many lines away from the initialization and loop condition. The increment won't always be as obvious as "i++" and can involve function calls. With a well-written for loop, you can see all of that at a glance.
- zozbot234 5y ago> With a well-written for loop, you can see all of that at a glance. Yes, this is ultimately about the push for structured programming, which involves creating a pattern language of loops with "clean" semantics. Grouping the loop advancing statement with the other parts of a general loop construct helps wrt. recognition of known looping patterns.
- dataflow 5y agoKeep in mind they're not always equivalent. If you have 'continue' in the middle then the for loop will still perform the increment but the while loop won't.
- google234123 5y agoThis bites me a few times a year.
- dataflow 5y agoIMHO the way to avoid it is to avoid nonlinear [1] control flow. If you can convince yourself that early returns, continue, etc. are harmful, you'll avoid the pitfalls that come with them. It's an unpopular position to take, though. [1] I might be sloppy with the terminology here, but you catch my drift.
- jraph 5y agoI do think early returns, continue, etc are a good source of clarity. Sometimes, code that avoids early returns seems convoluted and can use more variables, be more verbose and require more effort to be understood. In for loops, you need to understand that this complicated condition is there to replace a break. Not my cup of tea.
- dataflow 5y agoYeah I get that, that's always the argument. I'm not an absolutist on this either (I do sprinkle continue/return into my code once in a while when it really seems worth it; even the same with the occasional goto), but in general I feel people are way more liberal with these than they should be (except goto, which people avoid at all costs no matter how complicated the code becomes, thus erring toward the other extreme). Never been able to convince many others though; the arguments never get anywhere when person A insists X is more readable and person B insists Y is more readable.
- jraph 5y agoAt the end, readability depends heavily on habits. You can know that, but you feel it when you work on various codebases with different people / switch teams. So, yes. Nobody is right or wrong in these topics. Moreover, there are situations where one is clearly better than the other. If you need to (formally) verify your code, depending on the method, maybe early returns are to be avoided.
- WalterBright 5y agoThe D compiler simply rewrites while loops into for loops, that way I only had to write one semantic routine for the for loop.
- deleted 5y ago[deleted]
- edgyquant 5y agoIn this example maybe, but in practice the for loop looks cleaner (imo obviously.)
- deleted 5y ago[deleted]
- iib 5y agoI think there is also an extra scope embedded in the `for`. So enclosing it would be closer: { int i = 0; while(i < 10) i++; }