## FOR

### M[UMPS] by Example

Introduced in the 1977 ANSI M[UMPS] language standard.

For I=1:1 Write I Quit:I>2  Write "*"
To be printed: 1*2*3
Final value of I is 3.

For I=1:1:3 Write I
To be printed: 123
Final value of I is 3.

For I=1:1:3,"ABC",2,4:2:7 Write " ",I
To be printed: 1 2 3 ABC 2 4 6
Final value of I is 6.

For I=1:1:5 Set:I=3 I=–6  Set:I=–3 I=9 Write " ",I
To be printed: 1 2 –6 –5 –4 9
Final value of I is 9.

Note that the fact that I receives a value smaller than the lower limit is no reason to terminate the loop.

Note that the fact that I receives a value greater than the upper limit is no reason to terminate execution of the scope of the For command.

For I=1:1:4 Write I For J=1:1:3 Write "*"
To be printed: 1***2***3***4***
Final value of I is 4, the final value of J is 3.

For I=3:5:0 Write I
Nothing is printed. Final value of I is 3.

Set X=1,Y=2,Z=7 For I=X:Y:Z Write I
To be printed: 1357

X   For I=1:1:3 For J=1:2:5 Write " ",I,J If I=2 Goto Y
Y   Write ", jumped to Y." Quit

When this routine is executed using the command Do X
will be printed: 11 13 15 21, jumped to Y.
The Goto command terminates both For commands.

X   For I=1:1:3 For J=1:2:5 Write " ",I,J If I=2 Do Z
Y   Write " Y" Quit
Z   Write " Z" Goto Y

When this routine is executed using the command Do X
will be printed:
11 13 15 21 Z Y 23 Z  Y 25 Z Y 31 33 35 Ready.
The Goto command in line Z has no implications for any For command.

Set K=0,L=0 For K=1:1:5,L=3:3:9 Write " ",K,L
To be printed: 10 20 30 40 50 00 30 60 90
Note that the value of L is not changed because of the For command. The expression L=3 is a truth valued expression with the value 0 (FALSE).

For K=1:1:3 For L=4:2:8 Write " ",K,L Quit:L=6
To be printed: 14 16 24 26 34 36
The Quit command only affects the For command with variable L as a counter.

Set STEP=2
For K=1:STEP:9 Write " ",K,"_",STEP Set STEP=STEP+K

To be printed: 1_2 3_3 5_6 7_11 9_18
Changing the value of local variable STEP has no implications for the step-size used by the M[UMPS]-system.

For K="A":1:4 Write " ",K
To be printed: 0 1 2 3 4
Note that the expression "A" will be evaluated as an nve (numeric valued expression), which causes it to be seen as a value of 0.

Set LIST="1*234*5*9876*abc*d*e*f*ghi*jkl",N=0
For I=1:1:\$Length(LIST,"*") Set N=N+1 Write !,N,": ",\$Piece(LIST,"*",I)

To be printed:
1: 1
2: 234
3: 5
4: 9876
5: abc
6: d
7: e
8: f
9: ghi
10: jkl

Set LIST="1*234*5*9876*abc*d*e*f*ghi*jkl",N=0
For I=1:1:\$Length(LIST,"*") Set N=N+1,I=I+1 Quit:N>4  Write !,N,": ",\$Piece(LIST,"*",I)

To be printed:
1: 234
2: 9876
3: d
4: f

Set LIST="1*234*5*9876*abc*d*e*f*ghi*jkl",N=0
For I=1:1:\$Length(LIST,"*") Set N=N+1,I=I+1 Quit:I>4  Write !,N,": ",\$Piece(LIST,"*",I)

To be printed:
1: 234
2: 9876

Typical code to iterate through a muli-level data structure would be:

Set sub1="" For  Set sub1=\$Order(name(sub1)) Quit:sub1=""  Do
. Set sub2="" For  Set sub2=\$Order(name(sub1,sub2)) Quit:sub2=""  Do
. . Set sub3="" For  Set sub3=\$Order(name(sub1,sub2,sub3)) Quit:sub3=""  Do
. . . ; Perform some action at this level
. . . Quit
. . Quit
. Quit

Examples with naked references:

For LVN=FROM:STEP:UNTIL
Set ^ABC(1,2)="reset naked indicator"
; naked indicator is now ^ABC(1,
For I=^(3,4):^(5,6):^(7,8)
; 1. fetch ^(3,4) = ^ABC(1,3,4)
; 2. fetch ^(5,6) = ^ABC(1,3,5,6)
; 3. fetch ^(7,8) = ^ABC(1,3,5,7,8)
; naked indicator is now: ^ABC(1,3,5,7,