
C's 'while' is spelled 'for' in Go - sunainapai
https://tour.golang.org/flowcontrol/3
======
UIZealot
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.

~~~
laurencerowe
> 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.

~~~
saghm
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.

~~~
Lammy
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](https://ruby-
doc.org/core-2.7.1/Kernel.html#method-i-loop)

------
erik_seaberg
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.

------
ResidentSleeper
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.

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

    
    
      for {
        ...
      }

~~~
klyrs
Looks like a minor simplification of

    
    
      for (;;) {
         ...
      }
    

I'm biased by my experience with c, but I do like the explicit punctuation.

