3 ms·
Interesting. I suppose you are probably right, but I would still argue it's confusing and doesn't actually line up with the semantics of the C-style for loop. I
by jgwil2 6y ago
Interesting. I suppose you are probably right, but I would still argue it's confusing and doesn't actually line up with the semantics of the C-style for loop. If it were a language like Python, wherein collections can actually be iterated over with a "for ... in ..." statement, then it might make more sense. But the C for loop just has an initializer, a break condition, and a statement executed between loops, so there's not necessarily a connection to the mathematical notation, even if it's often used to execute some function over a collection.
- whatshisface 6y agoI was curious so I did a bit of historical research. The "for" loop comes from ALGOL[0]. In ALGOL, the FOR statement supports syntax like "FOR V:= AEXP1, AEXP2,... DO". That's like the higher-level iterator syntax, except it's assigning V via iteration over a sequence of expressions (AEXP N) that is fixed at compile time. That's the context where FOR makes sense as a term. (Step 1: thesis) Now, ALGOL also happens to support a FOR-STEP-WHILE format, which works the same way as the C for-loop[0]. That makes less sense from a mathematical nomenclature perspective, but it is justified through analogy to the original FOR definition. When C, which was based in part on ALGOL, implemented for, it dropped the original meaning and kept the special case meaning because the latter was more useful, being dynamic instead of fixed at compile time. Hence, the for loop. (Step 2: antithesis) Eventually other languages implemented for with iterators, returning to the original ALGOL meaning of for, but synthesizing it with the dynamism of the C for loop. (Step 3: synthesis) [0] https://public.support.unisys.com/aseries/docs/clearpath-mcp-18.0/86000098-516/section-000026445.html https://public.support.unisys.com/aseries/docs/clearpath-mcp... Disclaimer: references to the Hegelian dialectic are for entertainment purposes only.