ITERATORS &
ITERABLE
PROTOCOLS
Concepts, Patterns, &
Implementations
GROUP 1
PROGRAMMING
LANGUAGES
ITERATORS
Are objects that allow you to access elements
of a collection one at a time, without needing
to know how the collection is structured.
GROUP 1
ITERABLE
PROTOCOL
Is a set of rules that lets an object return an
iterator, making it usable in loops (like for
loops).
GROUP 1
WHY ARE THEY
IMPORTANT IN
PROGRAMMING?
They provide a consistent and efficient way to
loop through data, improve code readability,
and work across different types of collections.
GROUP 1
GROUP 1
ITERATOR
CONCEPTS
is an object used to step through elements of
a collection one at a time.
it keeps track of the current position so you
don’t need to manage indexes manually.
works with many data types (lists, arrays,
strings, etc.).
GROUP 1
ITERATOR
PATTERN
a design pattern that provides a standard
way to access elements in a collection
without exposing its internal structure.
separates the logic of traversing data from
the actual data storage.
common methods in the pattern:
• hasNext() - checks if there are more
elements.
• next() - gets the next element.
GROUP 1
USING FOR
LOOPS &
VARIATIONS
GROUP 1
FOR LOOP
A traditional for loop uses an index to access elements.
Internally, many languages use iterators or indexing to
step through elements.
Example (Python):
GROUP 1
FOR EACH LOOP
Directly iterates over each element of an iterable
without manually handling indexes. The loop
automatically calls the iterator under the hood.
Example (Java):
GROUP 1
FOR...OF LOOP
Iterates over values of any iterable object (like arrays,
strings, maps). Uses the [Link] protocol.
Example (JavaScript):
GROUP 1
FOR...IN LOOP
Iterates over the keys/indexes of an object or array, not
the values directly. Iterator concept here is about
property enumeration.
Example (JavaScript):
WHILE LOOP GROUP 1
You manually control iteration using an iterator object.
You check condition (hasNext() or similar) before
fetching the next element.
Example (Java):
DO WHILE LOOP GROUP 1
Similar to while, but ensures the loop runs at least
once. Uses iterator checks inside the loop.
Example (Java):
GROUP 1
LANGUAGE -
SPECIFIED
IMPLEMENTATIONS
PYTHON GROUP 1
In Python, an iterator must define __iter__() and __next__()
Example (Python):
JAVA GROUP 1
In Java, Iterator interface with hasNext(), next() methods
Example (Java):
JAVASCRIPT GROUP 1
JavaScript uses [Link] to make objects iterable
Example (JavaScript):
C# GROUP 1
IEnumerable and IEnumerator
Example (C#):
GROUP 1
ADVANTAGES OF
ITERATORS
Unified way to access collections.
Improves code readability.
Hides internal structure of collections.
Works across multiple languages with similar
patterns.
GROUP 1
CONCLUSION
Iterators provide a consistent way to traverse
data.
Iterable protocols standardize this process.
Every modern language has its own
implementation.