FOR directive
Introduces a loop; subsequent statements define the contents of the loop, which is terminated by the directive ENDFOR.
Options
Parameters
Any number of parameter settings of the form identifier = list of data structures; the identifier is set up as a dummy which is then used within the loop to refer, in turn, to the structures in the list
Description
The FOR loop is a series of statements, or a block, that is repeated several times. The FOR directive introduces the loop and indicates how many times it is to be executed. In its simplest form FOR has no parameters, and the number of times is indicated by the NTIMES option. For example, to calculate the mean of three sets of data stored in the file attached to channel 2:
FOR [NTIMES=3]
READ [CHANNEL=2] X
CALCULATE Mean = MEAN(X)
PRINT Mean; DECIMALS=4
ENDFOR
The INDEX option allows you to record the number of the time (below one, two or three) that the loop is currently being executed.
FOR [NTIMES=3; INDEX=Count]
The parameters of FOR allow you to write a loop whose contents apply to different data structures each time it is executed. Unlike other directives, the parameter names of FOR are not fixed for you by GenStat: you can put any valid identifier before each equals sign. Each of these then refers to a GenStat dummy structure; so you must not have declared them already as any other type of structure. The first time that the loop is executed, they each point to the first data structure in their respective lists, next time it is the second structure, and so on. The list of the first parameter must be the longest; other lists are recycled as necessary. You can specify as many parameters as you need. For example
FOR Ind=Age,Name,Salary; Dir='descending','ascending'
SORT [INDEX=Ind; DIRECTION=#Dir] Name,Age,Salary
PRINT Name,Age,Salary
ENDFOR
is equivalent to the sequence of statements
SORT [INDEX=Age; DIRECTION='descending'] Name,Age,Salary
PRINT Name,Age,Salary
SORT [INDEX=Name; DIRECTION='ascending'] Name,Age,Salary
PRINT Name,Age,Salary
SORT [INDEX=Salary; DIRECTION='descending'] Name,Age,Salary
PRINT Name,Age,Salary
printing the units of the text Name, and variates Age and Salary, first in order of descending ages, then in alphabetic order of names, and finally in order of descending salaries.
You can put other control structures inside the loop. So, for example, you can have loops within loops.
When you are using loops interactively, you may find it helpful to use the PAUSE option of SET to requests GenStat to pause after every so many lines of output. Another useful directive is BREAK, which specifies an explicit break in the execution of the loop.
Options: NTIMES, INDEX.
Parameters: names defining the dummies used within the loop.