Anytime you have a collection of things you will need some mechanism to systematically step though the items in that collection. As an everyday example, consider the television remote control, which lets us
iterate over various television channels. Similarly, in the programming world, we need a mechanism to systematically step through a collection of software objects. The mechanism used for this purpose is known by various names, including index (for iterating over an array), cursor (for iterating over the results of a database query), enumeration (in early versions of Java), and iterator (in more recent versions of Java).
I first learned to program in an early version of FORTRAN, where the only data structuring capability was an array. I quickly learned how to iterate over an array using an index and a DO-loop. From there it was only a short mental leap to the idea of using a common index into multiple arrays to simulate an array of records. Most programming languages have features similar to arrays, and they support straightforward looping over arrays. But modern programming languages also support more complex data structures such as lists, sets, maps, and trees, where the capabilities are made available via public methods but the internal details are hidden in private parts of the class. Programmers need to be able to traverse the elements of these data structures without exposing their internal structure, which is the purpose of iterators.
The Iterator pattern
An iterator is a mechanism that permits all elements of a collection to be accessed sequentially, with some operation being performed on each element. In essence, an iterator provides a means of "looping" over an encapsulated collection of objects. Examples of using iterators include
Visit each file in a directory (aka folder) and display its name.
Visit each node in a graph and determine whether it is reachable from a given node.
Visit each customer in a queue (for instance, simulating a line in a bank) and find out how long he or she has been waiting.
Visit each node in a compiler's abstract syntax tree (which is produced by the parser) and perform semantic checking or code generation. (You could also use the Visitor pattern in this context.)