The various kinds of Do commands iterate over a block of code. A dash is coded at the end of a code block within a Do loop. Do loops and If blocks can be nested within each other. If nested blocks end at the same point, the dashes and dots (the terminator for an If block) can all go on the same line. The right most terminator matches the inner most block. This way the terminators align vertically with their corresponding Do and If keywords.
DO <Statement> IF <Condition> DO <Statement> &Block of code> - . -
The Undo command will immediately exit a loop and program execution continues after the loop terminator. An optional condition can be coded on an Undo Command so that the progam only exits when the condition is met.
DO <Statement> : UNDO IF Condition : UNDO : -
An unconditional Undo command can also branch out of multiple nested loops. To do this add a dash after the Undo keyword for each level you'd like to escape.
DO <Statement> DO <Statement> DO <Statement> IF <Condition> UNDO -- - . <Some more commands> - : The Undo will jump here. -
A Do-Always loop iterates indefinately. To exit an unconditional loop you either need to use an Undo command or raise an exception using an Assert command.
DO always
A Do-While command loops as long as the condition is met.
DO while Condition
The Do-Until command loops until the condition is met.
DO until Condition
The Do-Times command evaluates an expression and iterates that many times. The expression is a signed Word or Cell expression. If the expression is untyped (often a constant) then it defaults to a Word type. The loop body is skipped if the expression value is zero or negative. The number of iterations is computed once before entering the loop and does not change even if variables in the expression are modified within the loop.
DO Expression times
This form lets you iterate a variable over a range of values. An optional By clause specifies the step size; which defaults to one if the By clause is ommitted. Additional details are on the Iterate Over a Range page.
DO Numeric [= Expression] to Expression [by Expression]
Use this command to iterate over a string from the first through the last character. The iterator variable, Character, is an integer set to each character in the string.
DO Character in <String Expression> [with Index]
The optional With clause designates a variable that counts each iteration starting with one for the first loop. For details see this page on Iterating Over a String.
This construct iterates a pointer over consecutive array elements. The optional subscipt designates the first element to scan. If the To clause is coded it determines the index to the last element to scan. The optional By clause is always -1 and scans elements in reverse order. For details see this page on Iterating Over an Array.
DO @Pointer in Array [Subscript] [to Expression] [by -1]
A Sequence is a function that can be called repeatedly to return the next value in a series. When invoked in a loop an iterator variable or pointer is set to the next value in the series. The iterator must have the same type as the Exit parameter declared in the Sequence procedure. For details about Sequences see this page on Iterating Over a Sequence, the page on writing a Sequence Procedure, and this page on taking individual steps though a sequence using the Churn Command.
DO [@]Result from Sequence( [Argument, ...] ) [with It]
The optional With clause designates a variable that is used as a handle that references the current state of the Sequence. While the Sequence is active it can be passed as a parameter to a subroutine or it can be used to resume a partially completed Sequence that has been suspended. Without the With clause the Sequence cannot be resumed after leaving the loop.
If you use the With clause and the loop exits via an Undo command then the Sequence will be suspended. Here the It variable is a handle for an active sequence that has been suspended. This second loop will resume iterating from the point where it left off.
DO [@]Result from It
This next form of Sequence loop is a convenient way to iterate over a structure such as a container. The Variable is a Class structure that contains a Sequence procedure with the same name as the structure that iterates over the structure. For example, if the Variable is a structure for a Stack container then a Sequence procedure also named Stack can iterate over elements in the stack structure.
DO [@]Result from Variable [with It]