5 ms·
Counterexample: as a beginner, I think knowing English made "for" harder to understand: for loops in C don't seem to bear much relation to the meaning of "for"
by jgwil2 6y ago
Counterexample: as a beginner, I think knowing English made "for" harder to understand: for loops in C don't seem to bear much relation to the meaning of "for" in common usage: yes, there is a condition that initiates the loop, but what about the other two statements? For me it was actually something I had to de-program from my brain before I could understand how they actually worked.
- whatshisface 6y agoI think it's rooted in mathematical phrases like "for all x in the set S..."
- jgwil2 6y agoInteresting. 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.
- Y_Y 6y agoThat's the problem though, a C for loop doesn't really work like that. We only recently got something close with the "range-based for" in C++, and while python's list comprehensions do pretty much exactly what I think mathematical "for" should, it re-uses the keyword in different ways elsewhere.
- btilly 6y agoHow recent an innovation this is depends on your language. It was added in C++11 so it looks recent there. But the syntax was in Perl 4 back in 1991. And someone who knows programming history can probably find older examples still in other languages.
- moistbar 6y agoI think "for" works better linguistically when taken in the context of a "for-each" loop, where my mental pseudocode becomes "for each of these, do this." It's a touch Yodalike, but it gives me a clearer expectation of what's happening and how long it's going to happen than I get from a C-style for loop.