COBOL | Java |
---|---|
class-id sillyCount. working-storage section. 01 c binary-long value 1 static. method-id main static. 01 i binary-long. procedure division using by value cmds as string occurs any. *> Use perform to loop over an iterator perform varying i through self::doCount() display i end-perform end method. *> Create an iterator by defining a member as an iterator *> Using iterator-id instead of method-id iterator-id doCount static. *> The return value if the iterator is defined using the *> yielding keyword procedure division yielding j as binary-long. perform until false add 1 to c move c to j *> Until the iterator is stopped, it will yield on *> a goback verb goback *> then start again directly after the goback *> on the next invocation of the iterator multiply c by 10 giving j if c less then 50 goback else *> Stop iterator - as it says *> Control goes back to the calling routine. stop iterator goback end-if end-perform *> COBOL will implicitly stop iterator at the *> end of the iterator definition. *> In this example – the code never gets here. end iterator. end class. |
In Java there is no YIELD feature, so the iterator implementation has to take care of state. |
Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.