[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.14 FOR

In its argumentless form, repeatedly executes the remainder of the line on which FOR was encountered until a QUIT, GOTO, or end-of-line is encountered, provided the optional postcondition evaluates to true or is omitted.

Syntax

FOR[:postcondition]

Non-Standard Behavior

When $DIALECT is set to FREEM, FreeM allows a postcondition on FOR. Much like postconditions on ELSE and IF, this is explicitly forbidden in the standard. The expression contained in the postcondition is evaluated on each iteration of the FOR loop, and if it does not evaluate true, the loop will be immediately exited. The effect is roughly similar to WHILE constructs present in other languages, but absent from standard M.

As with all non-standard features of FreeM, please exercise caution when using this feature, especially in code that is expected to run in other, less preternaturally-inclined M implementations.

In its sentinel form, repeatedly executes the remainder of the line and sets a sentinel variable on each iteration, provided the optional postcondition evaluates to true or is omitted.

On the first iteration of the loop, glvn will be set to initalizer-expression. On each subsequent iteration, glvn will be incremented by increment-expression, and the loop will terminate when glvn meets or exceeds the value of max-expression.

Syntax

FOR[:postcondition] glvn=initializer-expression:increment-expression:max-expression

Example

USER> FOR I=1:1:10 WRITE I,!

1
2
3
4
5
6
7
8
9
10

USER> FOR I=2:2:10 WRITE I,!

2
4
6
8
10

In its explicit parameter form, a variable is set to each of a series of explicit values, once per iteration, provided the optional postcondition evaluates to true or is omitted. The loop terminates when no more values are available.

Syntax

FOR[:postcondition] glvn=expr1[,..exprN]

Example

USER> FOR I=60,"FOO",-3,"George",1450,$HOROLOG WRITE I,!

60
FOO
-3
George
1450
66106,52388

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on November 11, 2022 using texi2html 5.0.