Skip to main content

This is documentation for Caché & Ensemble. See the InterSystems IRIS version of this content.

For information on migrating to InterSystems IRIS, see Why Migrate to InterSystems IRIS?

While and Do/While Constructs

You can also use the While or Do/While constructs, which act like the argumentless For in that they repeat sections of code, and terminate based on a condition. They only differ in whether the condition is evaluated before the entire code block (While) or after (Do/While). Like For, a Quit within the code block also terminates the loop. Here is the syntax of the constructs:

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>

The fibonacci.mac code:

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
    }
Feedback