There are many ways of expressing iteration over sets of numbers in Pop-11, including using repeat ... endrepeat, while ... endwhile, and until ... enduntil. However, it is usually "for" loops that are most convenient. These allow a variable to take successive values in a series of numbers defined by repeatedly adding or subtracting a fixed amount.
The basic syntactic form provided to support this kind of iteration is the following (where line-breaks are equivalent to spaces):
for <variable> from <number> by <number> to <number> do <actions> endfor;However the "from" and "by" components may be omitted where the number in question is 1. So the following forms are also permitted:
for <variable> from <number> to <number> do <actions> endfor (Default: "by 1") for <variable> by <number> to <number> do <actions> endfor (Default: "from 1") for <variable> to <number> do <actions> endfor (Defaults: "from 1", "by 1")Examples of arithmetical for loops are:
vars x; for x to 10 do spr(x) endfor; 1 2 3 4 5 6 7 8 9 10 for x by 0.5 to 5 do spr(x) endfor; 1 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 for x by 1/3 to 5 do spr(x) endfor; 1 4_/3 5_/3 2 7_/3 8_/3 3 10_/3 11_/3 4 13_/3 14_/3 5 for x from 1 to 6 do spr(x) endfor; 1 2 3 4 5 6 for x from 19 by -1 to 8 do spr(x) endfor; 19 18 17 16 15 14 13 12 11 10 9 8 for x from -5 by -0.6 to -9 do spr(x) endfor; -5 -5.6 -6.2 -6.8 -7.4 -8.0 -8.6For loops may be nested. For example, to make a list of all possible pairs of numbers between 3 and 5 do
vars x, y; [% for x from 3 to 5 do for y from 3 to 5 do [^x ^y] endfor endfor %] => ** [[3 3] [3 4] [3 5] [4 3] [4 4] [4 5] [5 3] [5 4] [5 5]]
When it is known that the values of the loop variable and the increment or decrement will all be integers, then it is permissible to use the fast_for instead of for, e.g.
vars x; fast_for x from 1 by 300 to 10000 do x endfor => ** 1 301 601 901 1201 1501 1801 2101 2401 2701 3001 3301 3601 3901 4201 4501 4801 5101 5401 5701 6001 6301 6601 6901 7201 7501 7801 8101 8401 8701 9001 9301 9601 9901Using this form will eliminate some of the run-time checks that are otherwise required. However, it is risky in that errors may not be detected. (See HELP EFFICIENCY).
For sequences of numbers not defined by constant intervals it may be convenient to use the following form:
for <action> step <action> till <condition> do <actions> endforFor example to produce a list of the powers of 2 less than 10000 do
vars x; [% for 2 -> x step x * 2 -> x; till x >= 10000 do x endfor%] => ** [2 4 8 16 32 64 128 256 512 1024 2048 4096 8192]