Examples of For

Example:

        GTM>KILL i FOR i=1:1:5 WRITE !,i
        1
        2
        3
        4
        5
        GTM>WRITE i
        5
        GTM>
        

This FOR loop has a control variable, i, which has the value one (1) on the first iteration, then the value two (2), and so on, until in the last iteration i has the value five (5). The FOR terminates because incrementing i would cause it to exceed the limit. Notice that i is not incremented beyond the limit.

Example:

        GTM>FOR x="hello",2,"goodbye" WRITE !,x
        hello
        2
        goodbye
        GTM>
        

This FOR loop uses the control variable x and a series of arguments that have no increments or limits. Notice that the control variable may have a string value.

Example:

        GTM>FOR x="hello":1:-1 WRITE !,x
        GTM>ZWRITE x
        x=0
        GTM>
        

Because the argument has an increment, the FOR initializes the control variable x to the numeric evaluation of "hello" (0). Then, GT.M never executes the remainder of the line because the increment is positive, and the value of the control variable (0) is greater than the limiting value (-1).

Example:

        GTM>FOR y=-1:-3:-6,y:4:y+10,"end" WRITE !,y
        -1
        -4
        -4
        0
        4
        end
        GTM>
        

This FOR uses two limited loop arguments and one value argument. The first argument initializes y to negative one (-1), then increments y to negative four (-4). Because another increment would cause y to be less than the limit (-6), the first argument terminates with y equal to negative four (-4). The second argument initializes the loop control variable to its current value and establishes a limit of six (6=-4+10). After two iterations, incrementing y again would cause it to be greater than the limit (6), so the second argument terminates with y equal to four (4). Because the final argument has no increment, the FOR sets y to the value of the argument, and GT.M executes the commands following the FOR one more time.

Example:

          GTM>S x="" F S x=$O(ar(x)) Q:x="" W !,x
        

This example shows an argumentless FOR used to examine all first level subscripts of the local array ar. When $ORDER indicates that this level contains no more subscripts, the QUIT with the postconditional terminates the loop.