Button for 1977 Button for 1984 Button for 1990 Button for 1995 Button for MDC Button for notes Button for examples

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.

Typically, M[UMPS] commands can have multiple arguments, separated by commas. The For command, however, can have at most one forparameter, which would look like LocalName=ListOfValues.
As an example: For I=1,2,3,K=2:2:5 Write " ",I
In this context, the expression K=2 is evaluated as a logical value, which means that this code will return one of three possible outcomes:

  1. If the value of K happens to be 2:
    1 2 3 1 3 5
  2. If the value of K happens to be anything else than 2:
    1 2 3 0 2 4
  3. If the variable K happens to be not defined:
    1 2 3 <undefined local variable: K>

Now, of course: For I=1,2,3 For K=2:2:5 Write " ",I
will always return 1 1 2 2 3 3

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 a 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
    Write "Ready." Quit
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
    Write " Ready." Quit
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,

Button for 1977 Button for 1984 Button for 1990 Button for 1995 Button for MDC Button for notes Button for examples

Copyright © Standard Documents; 1977-2024 MUMPS Development Committee;
Copyright © Examples: 1995-2024 Ed de Moel;
Copyright © Annotations: 2003-2008 Jacquard Systems Research
Copyright © Annotations: 2008-2024 Ed de Moel.

The information in this page is NOT authoritative and subject to be modified at any moment.
Please consult the appropriate (draft) language standard for an authoritative definition.

Some specifications are "approved for inclusion in a future standard". Note that the MUMPS Development Committee cannot guarantee that such future standards will indeed be published.

This page most recently updated on 09-Jan-2024, 15:14:56.

For comments, contact Ed de Moel (demoel@jacquardsystems.com)