Skip to main content

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). You can also use Quit, Return, and Continue with conditions inside the code block just as you can with For. Here is the syntax of the constructs:


do {code} while (condition)
while (condition) {code}

Examples of these constructs:

VS Code - ObjectScript


/// examples for ObjectScript Tutorial
Class ObjectScript.Examples
{

/// generate Fibonacci sequences
ClassMethod Fibonacci()
{
    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
    }
}
}
Testing using the Terminal


USER>do ##class(ObjectScript.Examples).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
USER>
FeedbackOpens in a new tab