Why don't we call the for loop what it really is, a foreach loop?

BartC bc at freeuk.com
Tue Sep 13 19:20:22 EDT 2016


On 13/09/2016 22:20, Ian Kelly wrote:
> On Tue, Sep 13, 2016 at 2:57 PM,  <rgrigonis at gmail.com> wrote:
>> It would help newbies and prevent confusion.
>
> Ada uses "for".
> C++11 uses "for".
> Dart uses "for".
> Go uses "for".
> Groovy uses "for".
> Java uses "for".
> JavaScript uses "for".
> MATLAB uses "for".
> Objective-C uses "for".
> Pasceal uses "for".
> Perl moved from "foreach" to just "for" in Perl 6.
> Ruby uses "for".
> Scala uses "for".
> Swift uses "for".

And Fortran uses "do".

My own language uses "for", "forall" and "foreach". (Apparently PHP also 
has a choice, according to another post.)

"for" only iterates over an integer sequence. "forall" over the values 
in an object (such as a list), similar to Python. (And "foreach" breaks 
apart certain kinds of object, such as the bits in an integer, and 
iterates over those).

Probably "for", "forall" and "foreach" could be combined (perhaps with a 
conversion to drive the the kind of iteration desired), but I think it's 
useful to see "for" and /know/ it's a basic loop. It also makes it 
easier to optimise, as well as allowing a stripped-down version that 
only counts, or repeats forever).

Value         Iterates over:
               For:         Forall:       Foreach:
1..3          1,2,3        1,2,3         --
(10,20,30)    1,2,3        10,20,30      --
"ABC"         1,2,3        "A","B","C"   65,66,67   (works with both)
100           0,1,2...63   --            0,0,1,0,0,1,1,0,...0

Getting back to Python, it only has one kind of for-loop and it has 
decided to call it "for". It's just one minor thing (among many) that 
has to be learned.

-- 
Bartc



More information about the Python-list mailing list