The FOR command provides a looping mechanism in GT.M. A FOR command does not generate an additional level on the GT.M invocation stack.
The format of the FOR command is:
F[OR][lvn=expr[:numexpr1[:numexpr2]][,...]]]
Because FOR is a conditional command, it does not support a command postconditional.
The scope of the FOR is the remainder of the line.
When the FOR has no argument, at least two (2) spaces must follow the command to separate it from the next command on the line. This specifies a loop that must be terminated by a QUIT, HALT, GOTO, or ZGOTO.
The optional local variable name specifies a loop control variable delimited by an equal sign (=). A FOR command has only one control variable, even when it has multiple arguments.
When initiating the FOR, GT.M assigns the loop control variable the value of the expression. When only an initial value appears, GT.M executes the remainder of the line once for that argument without forcing the control variable to be numeric.
If the argument includes an increment and, optionally, a terminator, GT.M treats the initial expression as a number.
The optional numeric expression after the first colon (:) delimiter specifies the increment for each iteration. The FOR command does not increment the control variable on the first iteration.
The optional numeric expression after the second colon (:) delimiter specifies the limiting value for the control variable. This expression is evaluated only when the control variable is initialized to the corresponding initial value, then used for all subsequent iterations.
GT.M does not execute the commands on the same line following the FOR if:
The increment is non-negative and the initial value of the control variable is greater than the limiting value.
The increment is negative and the initial value of the control variable is less than the limiting value.
After the first iteration, GT.M does not alter the control variable and ceases execution under the control of the FOR if:
The increment is non-negative, and altering the control variable by the increment would cause the control variable to be greater than the limiting value.
The increment is negative, and altering the control variable by the increment would cause the control variable to be less than the limiting value.
When the FOR has multiple arguments, each one affects the loop control variable in sequence. For an argument to gain control, no prior argument to the FOR can have an increment without a limit.
Increments and limits may be positive, negative, an integer, or a fraction. GT.M never increments a control variable "beyond" a limit. Other commands may alter a control variable within the extended scope of a FOR that it controls. When the argument includes a limit, such modification can cause the FOR argument to yield control at the start of the next iteration.
The scope of a FOR can be extended with DO or XECUTE commands.
GT.M terminates the execution of a FOR when it executes an explicit QUIT or a GOTO (or ZGOTO in GT.M) that appears on the line after the FOR. FOR commands with arguments that have increments without limits and argumentless FORs can be infinite loops. Such FORs must terminate with a QUIT or a GOTO within the immediate scope of the FOR. Also, such FORs can, but seldom, terminate by a HALT within the scope of the FOR as extended by DOs, XECUTEs, and extrinsics. FORs terminated by such commands act as "while" or "until" control mechanisms.