Skip to main content

This is documentation for Caché & Ensemble. See the InterSystems IRIS version of this content.Opens in a new tab

For information on migrating to InterSystems IRISOpens in a new tab, see Why Migrate to InterSystems IRIS?

WHILE 文と DO/WHILE 文

While 文や Do/While 文を使用して、引数なしの For 文と同様のコードを繰り返し実行し、条件を元に終了させることができます。WhileDo/While の違いは、条件の評価をコード・ブロックの前に行うか (While)、後で行うか (Do/While) です。For 文と同様、コード・ブロック内に Quit コマンドを記述してループを終了できます。以下はその構文です。

do {code} while condition
while condition {code}
SAMPLES>do ^fibonacci

Generate Fibonacci sequence up to where? 100
1  2  3  5  8  13  21  34  55  89
1  2  3  5  8  13  21  34  55  89
SAMPLES>

fibonacci.mac コードは以下のとおりです。

fibonacci ; generate Fibonacci sequences
    read !, "Generate Fibonacci sequence up to where? ", upto
    set t1 = 1, t2 = 1, fib = 1
    write !
    do {
        write fib,"  "  set fib = t1 + t2, t1 = t2, t2 = fib
    }
    while ( fib '> upto )

    set t1 = 1, t2 = 1, fib = 1
    write !
    while ( fib '> upto ) {
        write fib,"  "  set fib = t1 + t2, t1 = t2, t2 = fib
    }
FeedbackOpens in a new tab