Skip to main content

For Construct

You use the For construct to repeat sections of code. The argument of For is a counter variable, assigned a starting value, an increment value, and an ending value. All values may be positive or negative, and are separated by colons. The code block following the For will repeat for each value assigned to the variable. Alternatively, the argument of For can be a variable set to a list of values. In this case, the code block will repeat for each item in the list assigned to the variable. For, like the other constructs, can have multiple arguments separated by commas. Both the counter and list argument forms can coexist within the same For. The syntax of the For construct is:


for variable = start:increment:stop {code}
for variable = val_1, val_2, ..., value_N {code}

Within the code block of a For, you can use the Continue command. This stops the current iteration, but the loop continues.

You can specify the counter argument form of For without an ending value, which would normally result in an infinite loop. But by placing a Quit or Return within the code block, you terminate the For. This approach gives you a counter that you can use within the repetitions, but allows you to control the For using a condition that is not based on the counter's value. If you don't need a counter at all, you can use an argumentless For; this also requires a Quit or Return within the code block. Using For in this way is equivalent to using the While or Do/While constructs with a condition equal to 1 (true).

So far, you've learned that Quit and Return are identical. But they behave differently when used inside the code block of a For.

  • Quit terminates the loop only.

  • Return terminates the loop and the current class method, procedure, or routine.

Examples of For:

VS Code - ObjectScript

/// examples for ObjectScript Tutorial
Class ObjectScript.Examples
{
 
/// demos of the For construct
ClassMethod For()
{
    for i = 1:1:8 {
        write !, "I ", i, " the sandbox."
    }
    write !!
    for b = "John", "Paul", "George", "Ringo" {
        write !, "Was ", b, " the leader? "
        read yn
    }
    write !!
    for i = 1:1 {
        read !, "Capital of MA? ", a
        if (a = "BOSTON") {
            write "...did it in ", i, " tries"
            quit
        }
    }
    write !!
    for i = 1:1 {
        read !, "Capital of TX? ", a
        continue:(a '= "AUSTIN")
        write "...did it in ", i, " tries"
        quit
    }
    write !!
    for {
        read !, "Know what? ", wh
        quit:(wh = "NO!")
        write "   That's what!"
    }    
}
}
Testing using the Terminal


USER>do ##class(ObjectScript.Examples).For()

I 1 the sandbox.
I 2 the sandbox.
I 3 the sandbox.
I 4 the sandbox.
I 5 the sandbox.
I 6 the sandbox.
I 7 the sandbox.
I 8 the sandbox.
 
 
Was John the leader? y
Was Paul the leader? n
Was George the leader? n
Was Ringo the leader? n
 
 
Capital of MA? BAHSTON
Capital of MA? WORCESTER
Capital of MA? SPRINGFIELD
Capital of MA? BOSTON...did it in 4 tries
 
 
Capital of TX? HOUSTON
Capital of TX? SAN ANTONIO
Capital of TX? AUSTIN...did it in 3 tries


Know what? WHAT?   That's what!
Know what? WHAT?   That's what!
Know what? WHAT?   That's what!
Know what? NO!
USER>
Feedback