Note that an iterator performs traversal and also gives access to data elements in a container, but does not perform iteration (i.e., not without some significant liberty taken with that concept or with trivial use of the terminology).An iterator is behaviorally similar to a database cursor.In procedural languages it is common to use the subscript operator and a loop counter to loop through all the elements in a sequence such as an array.
There are subtle differences and distinctions in the use of the terms "generator" and "iterator", which vary between authors and languages.
Some object-oriented languages such as C#, C (later versions), Delphi (later versions), Go, Java (later versions), Lua, Perl, Python, Ruby provide an intrinsic way of iterating through the elements of a container object without the introduction of an explicit iterator object.
Various types of iterators are often provided via a container's interface.
Though the interface and semantics of a given iterator are fixed, iterators are often implemented in terms of the structures underlying a container implementation and are often tightly coupled to the container to enable the operational semantics of the iterator.
Or other times they may be created by the collection object itself, as in this Ruby example: This iteration style is sometimes called "internal iteration" because its code fully executes within the context of the iterable object (that controls all aspects of iteration), and the programmer only provides the operation to execute at each step (using an anonymous function).
Languages that support list comprehensions or similar constructs may also make use of implicit iterators during the construction of the result list, as in Python: Iterators are a useful abstraction of input streams – they provide a potentially infinite iterable (but not necessarily indexable) object.
A loop counter, however, only provides the traversal functionality and not the element access functionality.
One way of implementing iterators is to use a restricted form of coroutine, known as a generator.
Several languages, such as Perl and Python, implement streams as iterators.