Previous Topic Next topic Print topic


Iterators

An iterator is a section of code that returns an ordered sequence of values. You can create an iterator by defining a member as an iterator, using iterator-id.

iterator-specification

iterator-header procedure-division

iterator-header

iterator-id-signatureThe method signature enables you to specify passing parameters and returning items within the header of certain elements. You must omit the procedure division header within the method if you use this signature. access-modifier attribute-clause

Example

Here we use an iterator-id paragraph GetEven to return the even numbers in the FibonacciArray

    01 m-FibonacciArray binary-long occurs any static.
    01 anyNumber    binary-long.
    procedure division 
       ...
       set content of m-FibonacciArray to (1 2 3 5 8 13 21 34 55 89 144)

        display "Even numbers"         
        perform varying evenNumbers through iterators::GetEven
            display evenNumbers
        end-perform

    iterator-id GetEven static.
    01 i binary-long.
    procedure division yielding res as binary-long.
        perform varying i through m-FibonacciArray
            if i b-and 1 = 0
                set res to i
                goback
            end-if
        end-perform
        stop iterator *> stops the iterator immediately
    end iterator.     *> stops the iterator implicitly 

See also the Iterators sample, which is available from Start > All Programs > Micro Focus Visual COBOL > Samples , under COBOL for JVM (Windows) or $COBDIR/demo (UNIX).

Further information

If you specify parameters in the iterator header, you must not include a procedure division header in the body of the iterator.

The STOP ITERATOR statement stops the iteration.

This statement is not needed if the next statement is END ITERATOR.

Previous Topic Next topic Print topic