Program control
A GenStat program consists of a sequence of one or more jobs. The first job starts automatically at the start of the program. Subsequent jobs can be initialized by the JOB and ENDJOB directives:
The whole program is terminated by a STOP directive:
Statements within a program can be repeated using a FOR loop. The loop is introduced by a FOR statement. This is followed by the series of statements that is to repeated (that is, the contents of the loop), and the end of the loop is marked by an ENDFOR statement. Parameters of the FOR directive allow lists of data structures to be specified so that the statements in the loop operate on different structures each time that it is executed.
GenStat has two ways of choosing between sets of statements. The block-if structure consists of one or more alternative sets of statements. The first set is introduced by an IF statement. There may then be further sets introduced by ELSIF statements. Then there may be a final set introduced by an ELSE statement, and the whole structure is terminated by an ENDIF structure. The IF statement, and each ELSIF statement, contains a single-valued logical expression. GenStat evaluates each one in turn and executes the statements following the first TRUE logical found; if none of them is true, GenStat executes the statements following the ELSE statement (if any).
The multiple-selection structure consists of several sets of statements. The first is introduced by a CASE statement. Subsequent sets are introduced by OR statements. There can then be a final, default, set introduced by an ELSE statement, and the end of the structure is indicated by an ENDCASE statement. The parameter of the CASE statement is an expression which must produce a single number. GenStat rounds this to the nearest integer, n say, and then executes the nth set of statements. If there is no nth set, the statements following the ELSE statement are executed (if any).
Sequences of statements can be formed into GenStat procedures for convenient future use. The use of a procedure looks just like one of the GenStat directives, with its own options and parameters, which transfer information to and from the procedure. Otherwise the procedure is completely self-contained. The start of a procedure is indicated by a PROCEDURE statement. Then OPTION and PARAMETER statements can be given to define the arguments of the procedure. These are followed by the statements to be executed when the procedure is called, terminated by an ENDPROCEDURE statement.
Any control structure (job, block-if structure, loop, multiple-selection structure or procedure) can be abandoned using an EXIT statement. Also, execution of any of these structures can be interrupted explicitly with a BREAK statement, or implicitly by using DEBUG. Once DEBUG has been entered, GenStat will produce breaks automatically at regular intervals, until it meets an ENDDEBUG statement.
Macros within a procedure are substituted as soon as they are met during the definition of the procedure. However, it is also possible to execute a set of statements (contained in a text) during execution of the procedure. This can also be useful within loops.
In some implementations of GenStat, it is possible to suspend the execution of GenStat and return to the operating system of the computer to execute commands, for example to list or edit files on the computer. Likewise, it may be possible to halt the execution of GenStat to execute some other computer program. The OWN directive provides another way of running a user's program from within GenStat. The OWN subroutine, within the Fortran code of GenStat, needs to be modified to call the program. The new code must then be recompiled and linked into a new version of GenStat.