Starting with version 1.23, the provided language has added support for iterators, allowing us to iterate over virtually anything!
Let’s look at the List type once more. Instead of using a method that returns all elements as a collection, iterators make this much more elegant and efficient.
Here is the full source code:
With this setup, we have defined a linked list structure and operations on it, including pushing new values and iterating over them.
Initialization: The INIT-PARA is called to initialize the list with some values.
Push Operation: The PUSH-PARA adds elements to the linked list. If the list is empty, it initializes the head and tail. Otherwise, it adds the element at the tail’s next pointer.
Print All Elements: The PRINT-PARA iterates over the list and prints each element.
Fibonacci Sequence Generation: The GEN-FIB-PARA generates a Fibonacci sequence up to the given limit and displays the numbers.
Here, iterators are simulated using loops and COBOL’s native capabilities. While COBOL does not have first-class support for iterator-like constructs, this setup demonstrates a close equivalent using structured programming techniques.
To run this program, simply compile it using a COBOL compiler and execute the compiled binary. You will see the list values and the Fibonacci sequence as output.