Iterators - in COBOL and C#

C# COBOL
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);
        }
     }

    // 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 < 50)
            {
                yield return c * 50;
            }
            else
            {
                // yield break marks the end of the iterator
                yield break;
            }
        }
    }
}
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.