# Ditch the for loop?

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm looking for a little input on a programming language design choice; mostly from Python users, but feedback is feedback. Python does not implement (as far as I can tell) the C-style for(init; condition; increment){} loop. Only a for-each style loop (and uses a range generator for the common 0->x case). The C-style loop doesn't really fit into the overarching language design for my project, and I can't think of a common case where for-each isn't good enough. So, should I ditch the for loop? And the bonus question for Python users: Has that design choice ever been annoying? In what sort of scenarios?

##### Share on other sites
I have found that most languages that can trace their roots to functional languages typically do not use for-loops. I would assume that this is because they prefer the functional way of iterating over lists using recursion or folding.

Specifically, in Ruby, I find that everything I would want to do with a for loop, I can typically do with either 'each,' (iterate over each element) 'map,' (change the element) or 'inject' (similar to folding) on the Array object.

For example
a = [1,2,3,4,5]a.each { |x| print x }b = a.map { |x| x*2 } #returns [2,4,6,8,10]a.inject(0) { |s,x| s+x } #returns 1+2+3+4+5

I can't think of a place where I would actually need a for-loop when I don't plan on iterating...

##### Share on other sites
i'm coming from c++ where i used to loop around everywhere.

nowadays, coding in c#, i only use the for loop for iterating over the 2d array of pixels on screen when doing raytracing. that's a place where i'm so used to it, i don't bother thinking about another way..

but else, yes, it gets less and less relevant.

espencially since the introduction of linq in c#, ordinary loops are really not that often useful anymore.

##### Share on other sites
Personally, I tend to use for loops in C# and other languages that offer something akin to a 'for each' for instances in which I don't want to iterate over the entire set, but also features a single terminating criteria. Thus, in my case, 'increment' wouldn't be anything like 'i++', but would instead be something else that doesn't do this sort of linear exploration. I find the for-loop syntax a bit more concise for making it clear how the loop progresses as compared to a while loop that doesn't have an explicit spot for where the update should go. Also in the case of languages like C# that treat 'foreach' as a linear whole-container iteration, I use 'for' where I am not iterating from index zero, or perhaps not iterating to the last index. I use 'while's instead where the update phase isn't very straight forward, or if there are a lot of really complicated loop terminating criteria that would make a for loop look like a mess. For linear iteration [of entire containers in the case of C# and kin], 'for-each' is fine.

The bottom line though is that the 'foreach' loop isn't quite strong enough on its own to deal with iterative control flow, and by extension the straight-forward 'for' loop that you see in most intro C/C++ texts that closely emulates the 'foreach' statement as is being discussed here isn't sufficient either. 'foreach' is certainly the most restrictive of all these constructs though.

I'm an old grouchy C programmer though... so my opinion that the for loop just plain looks nicer than a while loop, and is less restrictive than a for-each loop should be taken with a grain of salt.

##### Share on other sites
The main place I use the 'for i in range(blah):' construct in Python is when I have for instance 2 list that I want to iterate over at the same time

for i in range(len(list1)):	item1 = list1	item2 = list2	#do something

And I'm pretty sure there is a way in python to do that without using range (but I just don't know it). Other then that I don't miss C style for loops and I do a lot of python in my job.

[Edited by - grekster on January 8, 2009 5:14:20 AM]

##### Share on other sites
Quote:
 Original post by greksterThe main place I use the 'for i in range(blah):' construct in Python is when I have for instance 2 list that I want to iterate over at the same time*** Source Snippet Removed ***And I'm pretty sure there is a way in python to do that with using range (but I just don't know it). Other then that I don't miss C style for loops and I do a lot of python in my job.

for item1,item2 in zip(list1,list2):  #do something

Just FYI :)

##### Share on other sites
Quote:
Original post by mikeman
Quote:
 Original post by greksterThe main place I use the 'for i in range(blah):' construct in Python is when I have for instance 2 list that I want to iterate over at the same time*** Source Snippet Removed ***And I'm pretty sure there is a way in python to do that without using range (but I just don't know it). Other then that I don't miss C style for loops and I do a lot of python in my job.

*** Source Snippet Removed ***

Just FYI :)

Awesome! :D

##### Share on other sites
Just a data point: neither FORTRAN (or Fortran) nor COBOL had C-style for(;;) loops, and they were expressive enough.

The C-style for(;;) loop is really just a one-line way of expressing the general loop construct used in PDP assembly language (using the decrement-and-branch instruction). Considering you didn't even have the C++-style inline index declaration in C, it's just syntactic sugar. I doubt you would regret not providing it in a new language, expecially if you already have an expressive for-each-item-in-a-sequence function already.

##### Share on other sites
I would drop them too.

I am curious though. I've been following your journal and your language as it has grown. Is it possible in your language for the user to define a function that simulates such a loop?

Something that looked like this (I have never used your language, forgive the syntax):
for <start> to <end> step <increment> do (current) => void { /* stuff with current */ };

With the symbols changed to not clash with your keywords.

IIRC, you have some sort of "phrase" support, alongside generics and delegates/closures. Would this be possible do you think? It would be neat if it was possible even though it wasn't actually predefined by the language.

##### Share on other sites
Sure, it's actually feasible as is. Well, 'as is' in the design. Signatures don't work yet, and you can't define generic methods fully in source yet, but...

public static void     for each (IEnumerable<T> Container) do ( void(T) operation ){    local IEnumerator<T> itr = Container.GetEnumerator();    while( itr.MoveNext() ){        operation(itr.Current);    }}

Though I'm thinking of moving void(T) to a T -> void syntax, but that's details...

And instead of doing the explicit start to end, it'd be better to make that a separate generator.

There are a few minor issues with just using this. You'll always end up using => void. Right now, the closure requires the parameter to have a type declaration with it which might be a little unwieldy/verbose.

Again, minor things that might not be a huge deal in practice. It's interesting to see no great arguments in favor of the C-style loop.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 11
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013540
×