Skip to Content
Delete Icon
This forum is now read-only. Please use our new forums at discuss.codecademy.com.
0 points
Submitted by CharliD
7 years ago

Loops vs iterators: I don't get the difference

So far we’ve learned one way to repeat an action in Ruby: using loops. As is often the case in Ruby, however, there’s more than one way to accomplish a given task. In this case, it’s also possible to repeat an action using an iterator.

An iterator is just a Ruby method that repeatedly invokes a block of code. The code block is just the bit that contains the instructions to be repeated, and those instructions can be just about anything you like!

The simplest iterator is the loop method.

I don’t get the difference. What makes an iterator not a loop and vice versa?

Answer 52954953548c350dba0007b5

4 votes

Permalink

The difference is quite subtle indeed. Both loops and iterators are used to repeat a chunk of code. Loops are an ancient idea, they existed long before computers did. Almost every programming language has some kind of loops. Iterators are relatively new, and they only exist in a few languages such as Ruby.

Loops are both powerful and dangerous: it’s too easy to break your program by producing a so called infinite loop, if you fail to ensure that the termination condition (the one that must be true so the loop stops) really occurs. For instance, I was trying to count from a number (that the user entered) to 100 in steps of 10:

number = gets.chomp.to_i    
while number != 100
  puts number += 10
end

Do you see the problem? If the user types in 5, we have an infinite loop. Oops. OK, this is really easy to fix (just change the loop condition to be number < 100 instead), but you get the point. An infinite loop is impossible if we use an iterator instead:

(number..100).step(10).each {|n| puts n}

But that’s not the only good thing about iterators. They also allow you to write more expressive, clutter-free code. Because they abstract away from the nitty-gritty details of a loop. Compare by yourself:

# loop               vs.           iterator

i = 0                              5.times do
while i < 5                         
  puts "hello"                       puts "hello"
  i += 1
end                                end

Both pieces of code perform exactly the same task. But which one is easier to write, read, and to understand?

Similarly, with each, the perhaps most famous iterator of all:

words = ["one", "two", "three", "four"]

# loop               vs.           iterator

i = 0                              words.each do |w|
while i < words.length                         
  puts words[i]                      puts w
  i += 1
end                                end

Lastly, you can define custom iterators for your special looping needs, and Ruby provides some cool built-in ones:

list = "a b c d e f g h i j".split(" ")
# => ['a','b','c','d','e','f','g','h','i','j']

list.each_slice(3) do |group|
  puts group.join ""
end

# prints abc, def, ghi, j

Try to do the same with a while loop and you’ll see how great it is to have iterators.

3117 points
Submitted by Alex J
7 years ago

1 comments

CharliD 7 years ago

Thanks again! I’m not as advanced as being able to understand all the examples yet, but this does clear up my question rather well.