views:

703

answers:

16

Just thought about it. Is there a semantic distinction, or are we free to choose?

EDIT I accepted @roygbivs answer, because most of the answers suggested that it is a matter of taste which one to choose, and that it doesn't really change the meaning. In that case, since the through and the over don't carry any information at all (in that particular context), they can be left out.

+6  A: 

Both essentially mean the same!

So, you are free to choose whichever suits you.

Mahesh Velaga
+14  A: 

I would say iterate over, and this is the way I've always seen it written.

Cocowalla
However, in Google, “iterate through the collection” yields significantly more results than both “over” and without a preposition...
Timwi
A: 

Technically it's probably just "iterate" period. But its obvious what you mean in either case.

Josh Einstein
A: 

The important word is iterate, what filler you place with it really doesn't matter.

Amber
A: 

I picture "iterating through" something, the way I do a file cabinet. Maybe it's just personal preference though, given my mental image - I think "Iterate Over" is the more common usage.

rwmnau
+9  A: 

I think it depends on the object of the preposition; you iterate over the iterator, over the collection, over the container, over the list, over the range, etc. but you iterate through (the contents of) the list, through (the elements of) the container, through the (elements of ) the range, through (the elements of) the collection, etc. In almost all cases they are interchangeable; the only place I would use "over" but not "through" is with respect to the iterator, itself; but for any iterable object, I'd use either "over" or "through".

Michael Aaron Safyan
This distinction sounds the best. By the way, ''over'' sounds more superficial, like you're an outside consumer. But ''through'' sounds more personal, more intimate. Like maybe you're the implementer and you're writing the enumerator.
Mark Canlas
I have no problems (as a native English speaker) iterating over a list, range or collection. In fact, "iterate through" **always** sounds wrong to my ear. @Mark, iterating is by definition external. So over is always best.
jmucchiello
@jmucchiello, I believe we have a misunderstanding; note that I wrote "iterate over the range" and "through (the elements of) the range". I agree that over sounds more natural, but I think that "through" sounds ok in most cases, except for the iterator, itself, in which case "through" sounds much more awkard than "over".
Michael Aaron Safyan
I think Mark said it right. Over is outside and Through sounds inside. My problem is I don't think the iterator is ever inside and thus never think through is right, even in your instance Michael. If I have a list of cars, I will iterator over the list of cars. I would not say "I iterate through the cars of the container." or through the car elements of the container. or through the range of cars in the container. None of those sound good.
jmucchiello
+11  A: 

Through and Over are useless adverbs.

Examples:

  • While iterating this collection, my code throws an error.
  • This code iterates the collection.
  • Your assignment is to write code that iterates the collection.

It's like saying up north or down south. Up and Down are useless. Instead you should say, I am going north for the summer. or In the south, they grow cotton.

I feel like they're more prepositions than they are adverbs, but the definitions for either seem flexible and it depends on how you parse the sentence. Is it a phrasal verb that you're simplifying? Iterate over => iterate? Or do you iterate as an intransitive verb with a preposition, which then simplifies to a transitive verb?
Mark Canlas
As Mark has noted, "through" and "over" are prepositions. How would you say it, then? Do you simply "iterate the container"? That doesn't sound right at all to me.
Michael Aaron Safyan
+1, I agree @roygbiv. Btw, I'm from *down* south and I don't grow cotton...my application does.
dboarman
dboarman
This, this, a thousand times this.
Nick Lewis
+2  A: 

I would always say "iterate over" because it emphasizes the (relatively important) fact that the iterators themselves are not contained in the container that is the subject of the iteration.

To say "iterate through", to me, would imply that one were looking at each element of the container in turn, which is not really what is happening.

In contrast, saying "iterate over" implies to me that one is receiving an external handle to each element of the container in turn, which is really what is happening.

However, there's nothing wrong with "iterate through", and anyone who understands iterators would surely understand both phrases.

Tyler McHenry
A: 

I use both at random, sometimes in the same sentence. Dropping the connecting word is bad grammar and neither is meaningful.

Joshua
A: 

Dictionary says "iterate" is a transitive verb meaning to "say or do again; repeat".

Would you "repeat over" something you just said, or "repeat through" it? I think you'd just plain repeat it. Or just plain iterate it, as you prefer.

Having said that, if you're using "iterate" as some kind of synonym for "loop"... looping through data one iteration at a time, perhaps, then maybe you ARE "iterating through". This is a minefield!

thesunneversets
A: 

I think they are interchangeable for most practical purposes, but in my comments I personally tend to use them with a small distinction: I interpret to "iterate over" something to be similar to the sense that you would say "I'll look over it". That is, it's not a deep inspection: you are browsing over it looking for something. Conversely, to "iterate through" is to go through each and every one purposefully.

So if I was just searching for one item in a list I would say I was "iterating over" the list.....if I was applying some transformation to each item, it would be "iterate through".

MisterMister
+3  A: 

Not right or wrong, just some stats:

"iterate through"     340,000 search results
"iterate over"        287,000
"iterate thru"         35,000
—————————————————————————————
"iterate in"           75,800
"iterate on"           49,600
"iterate across"       44,300
"iterate along"         5,230
"iterate among"         2,300
"iterate inside"        1,570
"iterate within"        1,280
"iterate against"         709
"iterate amongst"         136
"iterate amidst"            2
KennyTM
+1 for rigorousness
Space_C0wb0y
A: 

Either is a rhetoical tautology. You should remove the superfluous preposition. As in

"This loop iterates the top 10 products"

or, perhaps

"I am iterating the answers looking for the one to accept"

serg10
A: 

I use "loop over". Simple.

AMissico
A: 

Common Lisp's LOOP macro uses for element in list, but for element across array (it's different so the compiler can generate more efficient code ... yeah, it's weird), suggesting that the preposition isn't really that important.

Ken
A: 

The summary text for System.Collections.Generic.IEnumerator GetEnumerator() says:

Returns an enumerator that iterates through the collection.

Personally I say through ... but really I don't think it matters which one you use.

Peanut