29 points · by sunainapai · 6y ago · 32 comments
Ever since its counterpart "do" was dropped from the for-statement, it's just been utterly nonsensical. I mean how on earth does the word "for" convey a loop?
With the recently designed languages like Go and Swift taking so many cues from the Pascal side of the ALGOL language family, how nobody thought to pick up "repeat" or "loop" is simply beyond me.
Many languages only use `for` to mean a for-each style loop where it reads quite naturally:
Python - `for n in range(1,10)`
Ruby - `for i in 0..10`
Rust - `for n in 1..10` (also picked up the bare `loop`)
Others support for-each style loops along with the less than ideal C-style for loop:
JavaScript - `for (n of [1,2,3])` (second attempt, for-in has too many gotchas.)
Java - `for (int n : {1,2,3})`
Otherwise I agree that `while` reads far more naturally. I guess they must have wanted to minimize the number keywords in Go.
``` (1..10).each do |i| ... end ```
"do" does end up getting used, but only as syntax for a "block" (similar to what other languages would call a lambda).
Ruby also has a shorthand for when you want to do something `n` times without caring about the iteration number:
``` 10.times do ... end ```
One final note is that loop you wrote above is actually different in Ruby than it is for Python and Rust; `x..y` is an inclusive range in Ruby, whereas `range(x, y)` in Python and `x..y` in Rust are exclusive on the right side. I think you might have realized that since you used different starting values for the other two, but you got it backwards; the Python and Rust loops will loop only nine times, whereas the Ruby one will loop 11 times. Ruby does have a syntax for exclusive ranges though; `x...y` is an inclusive range from `x` to `y - 1`. Similarly, in Rust, `x..=y` gives an inclusive range from `x` to `y`. I don't know of any way to do an inclusive range in Python other than manually, but someone with more Python knowledge might be able to provide something for that.
"while" is no better without the "do".
Your whole argument is nonsensical. Coding isn't English
Write a new language and you can name everything exactly how you please, if enough people care then they will adopt the language.
is how you'd say that in plain english. Some languages do use foreach as a keyword echoing that.
I don't know about a loop per se, but it's used in plain language when you want the same thing to be done to every element in a set. "Clean this item and put it away. Then do that for every other piece of cooking equipment."
It was actually originally borrowed from its cognate für in German:
>>The name for-loop comes from the English word for, which is used as the keyword in many programming languages to introduce a for-loop. The term in English dates to ALGOL 58 and was popularized in the influential later ALGOL 60; it is the direct translation of the earlier German für, used in Superplan (1949–1951) by Heinz Rutishauser, who also was involved in defining ALGOL 58 and ALGOL 60.
Without the "do" it's nonsensical. And that was exactly my point.
And "do ... while(x)" is a terrible construct because it hides the loop complexity far from the place where a programmer needs to see it.
Not at all. "do" helps a lot to make the code read like English and thus more understandable.
sum = 0
grob (i=0; i<10; i++) {
sum += i
}
bolver sum
Brilliant.for-range desperately needs to support some interface that would make user-defined types iterable.
I don't think it makes a big difference either way.
for {
...
} for (;;) {
...
}
I'm biased by my experience with c, but I do like the explicit punctuation.