Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Loops and Iterators Difference

Reply
Thread Tools

Loops and Iterators Difference

 
 
Soh Dubom
Guest
Posts: n/a
 
      10-30-2008
Hi. The way I understand, both loops and iterators are, 'loops', but the
sutil difference is that a loop is used to just loop several times and
an iterators loops several times and also access some attribute of the
element being loop.

I'm actually trying to find a better and clear explanation of the
difference between loop and iterator.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Stefan Lang
Guest
Posts: n/a
 
      10-30-2008
2008/10/30 Soh Dubom <>:
> Hi. The way I understand, both loops and iterators are, 'loops', but the
> sutil difference is that a loop is used to just loop several times and
> an iterators loops several times and also access some attribute of the
> element being loop.
>
> I'm actually trying to find a better and clear explanation of the
> difference between loop and iterator.


An iterator is an object that allows us to traverse the
elements of a data structure. In Ruby we usually use
"internal" iterators, that means we don't need to explicitly
manage an iterator object:

my_tree.each_node { |node| node.do_something }

Using an external iterator (in combination with a loop!) could look
like this:

iterator = my_tree.each_node_iterator
while iterator.has_next?
node = iterator.next
node.do_something
end

In both cases, the iterator allows us to traverse the data
structure without knowledge about its internals. Which frees
us to change the implementation later without changing
client code.

A loop simply means repeating a sequence of statements,
endlessly in the simplest case:

loop { do_something }

We could use a loop to traverse a data structure,
say a linked list:

item = people
while item
item.object.do_something
item = item.next
end

But this code breaks when we decide to use e.g. an array
instead of a list to store people.

Stefan

 
Reply With Quote
 
 
 
 
Soh Dubom
Guest
Posts: n/a
 
      10-31-2008
Thanks Stefan ... very good explanation
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing iterators to one past end() in for loops Nikos Chantziaras C++ 15 04-14-2011 06:45 PM
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
what the difference between these loops? doolittle Perl Misc 12 11-18-2006 12:04 AM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57