![]() |
![]() |
ここで、iterator-id 段落 GetEven を使用して、m-FibonacciArray に偶数を戻します。
class-id Fibonacci.Class1 static. data division. working-storage section. 01 m-FibonacciArray binary-long occurs any static. method-id FibEvens static. procedure division. set content of m-FibonacciArray to (1 2 3 5 8 13 21 34 55 89 144) display "Even numbers in the Fibonacci sequence:" perform varying evenNumber as binary-long through self::GetEven display evenNumber end-perform goback. end method. iterator-id GetEven yielding res as binary-long static. perform varying i as binary-long through m-FibonacciArray if i b-and 1 = 0 set res to i goback end-if end-perform stop iterator *> stops the iterator explicitly end iterator. *> end of method stops the iterator implicitly end class.
以下は同様の例ですが、self 構文を使用して現在のクラス (反復子があるクラス) から直接反復できるようにしたものです。
class-id Fibonacci.Class2. method-id main static (args as string occurs any). try declare max as binary-long = binary-long::Parse(args[0]) declare fibs = new Fibonacci(max) perform varying i as binary-long through fibs display i end-perform catch display "Bad command line" end-try end method. end class. class-id Fibonacci. 01 MaxNumber binary-long. method-id new (#max as binary-long). set MaxNumber to #max end method. iterator-id self yielding res as binary-long. set res to 1 exit iterator declare frst as binary-long = 0 declare second as binary-long = 1 perform until exit compute res = frst + second if res > MaxNumber stop iterator end-if move second to frst move res to second exit iterator end-perform end iterator. end class.
反復子のサンプルも参照してください。このサンプルは、[スタート > すべてのプログラム > Micro Focus Visual COBOL > Samples]、 の場合JVM COBOL 言語リファレンスの場合 (Windows) または $COBDIR/demo (UNIX)。 に用意されています。
反復子のヘッダーでパラメーターを指定する場合は、反復子の本体に手続き部の見出しを含めることはできません。
STOP ITERATOR 文は反復を停止します。
この文は、次の文が END ITERATOR の場合は必要ありません。