Skip to main content

For Construct

You use the For construct to repeat sections of code. The example shows the syntax of the argument of For: a 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.

SAMPLES>do ^forexample

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

Part 1 of the forexample.mac code:

forexample ; examples of the for construct
    for i = 1:1:8 {
        write !, "I ", i, " the sandbox."
    }
    write !!
    for b = "John", "Paul", "George", "Ringo" {
        write !, "Was ", b, " the leader? "
        read yn
    }
FeedbackOpens in a new tab