Iterators - in COBOL and C#

C# COBOL
using System.Collections.Generic;
class SillyCount
{
    static int c = 1;
        
    static void Main(string[] args)
    {
        // Use foreach to loop through an iterator
        foreach(int i in doCount())
        {
            System.Console.WriteLine(i);
        }
        System.Console.WriteLine("");
        SillyCount silly = new SillyCount();
        foreach(int i in silly)
        {
            System.Console.WriteLine(i);
        }

     }

    // C# lacks specific syntax to mark a method as an iterator
    // you have to create a method with the correct concrete form
    // of the IEnumerable interface and then use the yield return
    // syntax inside the method.
    static IEnumerable<int> doCount()
    {
        while (true)
        {
            c++;
            // C# uses an implicit return 'variable' rather than
            // the named one in COBOL. Yield return returns
            // the value as the current value of the iterator
            yield return  c;

            // then starts immediately after the 
            // yield return on the next invocation of 
  	    // the iterator
            if (c < 5)
            {
                yield return c * 10;
            }
            else
            {
                // yield break marks the end of the iterator
                yield break;
            }
        }
    }
    // Define an iterator for the current class
    public IEnumerator<int> GetEnumerator()
    {
        for (int i = 23 ; i >= 0; i -= 5)
        {
            yield return i;
        }
    }

}
      $set sourceformat(free)
class-id sillyCount.
working-storage section.
01 c binary-long value 1 static.

method-id main (cmds as string occurs any) static.
    *> Use perform to loop over an iterator
    perform varying i as binary-long through self::doCount()
        display i
    end-perform
    display space
    declare o = new self
    perform varying i as binary-long through o
        display i
    end-perform
end method.
           
*> Create an iterator by using iterator-id instead of method-id
iterator-id doCount static.
    *> The return value of 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, equivalent to exit iterator
        goback

        *> ...and then start again directly after the goback 
        *> on the next invocation of the iterator
        multiply c by 10 giving j
        if c less then 5
            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.

    *> Create an iterator directly for an instance of this class
    *> Note use of 'self' keyword instead of giving the iterator a name
iterator-id self yielding j as binary-long.
    perform varying j from 23 by -5 until j < 0
           exit iterator
    end-perform
end iterator.
end class.