When an iterator is called, it executes the statements in its body in order. If it executes a yield statement, control is returned to the caller. In this, the iterator is similar to a coroutine whose state remains persistent over multiple calls. Subsequent calls on the iterator resume execution with the statement following the yield statement. If an iterator executes quit or reaches the end of its body, control passes immediately to the end of the innermost enclosing loop statement in the caller and no value is returned.
Iteration is done with loop statements , used in conjunction with iterator calls.
In the absence of iterator calls, a loop statement simply executes an infinite loop. When a loop is entered, the execution state of all enclosed iterator calls is initialized. Just as a routine may provide a value when it returns, so too an iterator may provide a value when it yields. Note that each loop may contain more than one iterator call, thus providing much more flexibility than conventional languages. When any of the iterators terminates, the whole loop terminates, and execution continues at the next statement after the loop.