Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
C's 'while' is spelled 'for' in Go (golang.org)
29 points by sunainapai on Aug 10, 2020 | hide | past | favorite | 32 comments


It's kind of sad that "for" is the keyword programming languages settled on for the loop construct.

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.


> It's kind of sad that "for" is the keyword programming languages settled on for the loop construct.

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.


Interestingly, the more common way to write that in Ruby would be `(1..10).each { |i| ... }`, or (in the case of needing a multi-line block):

``` (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.


You can kinda use `do` for looping with a Block and Kernel#loop :v https://ruby-doc.org/core-2.7.1/Kernel.html#method-i-loop


It's been years since I last wrote Ruby so had completely forgotten that it's ranges were inclusive by default.


It's only natural if you already know that a for-statement is a loop.

"while" is no better without the "do".


And if statements need a corresponding "then" to make sense as well right?

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.


Python's "for customer in customers" can't be clearer in my opinion.


"for each customer in customers"

is how you'd say that in plain english. Some languages do use foreach as a keyword echoing that.


In math you have the "for all customers there exists a customer that". Maybe I should write up a formal INTERCAL proposal that this should be the proper loop construct in 2020, spelled out in plain Math English like that.


>I mean how on earth does the word "for" convey a loop?

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.

https://en.wikipedia.org/wiki/For_loop


> "Clean this item and put it away. Then do that for every other piece of cooking equipment."

Without the "do" it's nonsensical. And that was exactly my point.


I think there's an argument that "do" is just a placeholder for the body of the loop, i.e. "clean this item and put it away". If you flip the order that the body and the iteration are expressed so that it reflects what most programming languages use for syntax, it would be "for each piece of cooking equipment, clean it and put it away". There's no "do" here, nor is it needed.


There you need the word "each" to convey that it's a loop.

I'd certainly agree that "for each" is much better than "for" alone.


That was just one way of phrasing it. You could just as easily put it as, "For every piece of cooking equipment, clean it and put it away."


There you need the word "every" to convey that it's a loop.

I'd certainly agree that "for each" is much better than "for" alone.


"Do your homework" doesn't imply a loop either. Nor does "This could take a while"

It should not be a shock that the english language doesn't naturally have a single word that can express both a condition/duration as well as a command without any other helper words.

Do is superfluous. Everything in an imperative program is a request to do something.


> "Do your homework" doesn't imply a loop either.

But "for these subjects, do your home work" does.

> Nor does "This could take a while"

I never claimed that "while" alone is any better.

> It should not be a shock that the english language doesn't naturally have a single word that can express both a condition/duration as well as a command without any other helper words.

I never asked for a magic word. To the contrary, I was lamenting the lost of the helper word "do", making the "for" alone nonsensical.

> Do is superfluous. Everything in an imperative program is a request to do something.

I disagree. I think "for...do" makes things much clearer than "for" alone.


While we're being pedantic, "do" is a terrible keyword. I mean, everything in an imperitive program is a request to "do" something.

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.


The condition of a C-like do/while loop is exactly where the programmer needs to see it: where it's evaluated. Same with the plain while loop.


> "do" is a terrible keyword.

Not at all. "do" helps a lot to make the code read like English and thus more understandable.


All languages, both human and programming, have vestigial nonsense like this. All of them. It isn't possible for languages which are used to not have them, I would say.


Wish they'd called it `grob` and called `return` `bolver` so that we can accept they're just syntactic constructs instead of pointlessly assuming meaning from what are just syntactic constructs.

    sum = 0
    grob (i=0; i<10; i++) {
      sum += i
    }
    bolver sum

Brilliant.


I feel like this is what learning to program is like when english isn't your first language


I learned programming before I learned English. I was always confused by PRINT vs INPUT: which one reads and which one writes? So yeah, it could have been grob and bolver or anything.


Ironically, I was undone here by bad pedagogy. The text I was reading said something about "`getch` being the opposite of what you expected since you'd think it gets the character from the computer and prints it to the screen, while `putchar` does the opposite of what you expected and doesn't give you the chance to put anything into the computer".

Now, clearly whoever wrote that text had their own conception of the thing, but my mental model was that the computer is executing the program so it is getting the character from me with `getch` and it is putting the character on the screen with `putchar`. So the actual functionality seemed right.

But when you combined that with the text saying that it was counter-intuitive I was like "Oh okay, got to remember it was counter-intuitive. It's the opposite of what I'd think" and so the text induced me to misunderstand. Fortunately I was a child so I learned pretty rapidly that lots of people write total crap (irrespective of the conception, the author should not have pushed it so hard) and it didn't damage my mental model at all.


Oh man, trust me, by the time you start programming, you'll have already studied English. There's no way around it since non-English documentation is miserable at best.


It's a nice refinement over the C version, though do-while would also be convenient to handle paginated batches of work without unrolling the loop.

for-range desperately needs to support some interface that would make user-defined types iterable.


Looking at the syntax (as a Go beginner), I'm not really sure whether that's less or more to learn. Seems like one of those arbitrary language design decisions that don't make a huge difference either way.


Yup. They saved a keyword or two but now you need to count the semicolons to see if there is an increment action.

I don't think it makes a big difference either way.


Yeah when I picked up Go, the infinite loop caught me by surprise:

  for {
    ...
  }


Looks like a minor simplification of

  for (;;) {
     ...
  }
I'm biased by my experience with c, but I do like the explicit punctuation.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: