1. Home
  2. Program control

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:

JOB starts a Genstat job (ending the previous one if necessary)
ENDJOB ends a job

The whole program is terminated by a STOP directive:

 STOP ends a Genstat program

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.

FOR indicates the start of a loop
ENDFOR marks the end of a loop

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).

IF introduces a block-if structure
ELSIF introduces an alternative set of statements in a block-if structure
ELSE introduces a default set of statements for a block-if structure
ENDIF marks the end of a block-if structure

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).

CASE introduces a multiple-selection structure
OR introduces an alternative set of statements for a multiple-selection structure
ELSE introduces a default set of statements for a multiple-selection structure
ENDCASE marks the end of a multiple-selection structure

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.

 ROCEDURE introduces a procedure, and defines its name
OPTION defines the options of a procedure
PARAMETER defines the parameters of a procedure
CALLS lists library procedures called by a procedure
ENDPROCEDURE indicates the end of a procedure
WORKSPACE accesses “private” data structures for use in procedures

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. You can also issue a faults, warnings or messages.

EXIT exits from a control structure
BREAK suspends the execution of a control structure
ENDBREAK continues execution of a control structure, following a break
DEBUG can cause a break to take place after the current statement (and at specified intervals thereafter), or immediately after the next fault
FAULT evaluates a logical expression to decide whether to issue a diagnostic, i.e. a fault, warning or message
DISPLAY prints, or reprints, diagnostic messages

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.

EXECUTE executes the statements contained within a text

Other commands that may be useful in programs and procedures include the following:

CAPTION prints captions in standardized formats
COMMANDINFORMATION provides information about whether (and how) a command has been implemented
COUNTER increments a multi-digit counter using non base-10 arithmetic
GET accesses details of the “environment” of a Genstat job
SET sets details of the “environment” of a Genstat job
ENQUIRE provides details about files opened by Genstat
GETATTRIBUTE accesses attributes of structures
CHECKARGUMENT checks the arguments of a procedure
LIBEXAMPLE accesses examples and source code of library procedures
SETCALCULATE performs Boolean set calculations on the contents of vectors or pointers
SETRELATE compares the sets of values in two data structures
SPSYNTAX puts details about the syntax of commands into a spreadsheet
SYNTAX obtains details of the syntax of a command and the source code of a procedure
ASSIGN sets elements of pointers and dummies
DELETE deletes the attributes and values of structures
DUPLICATE forms new data structures with attributes taken from an existing structure
%LOG adds text into the Input Log window in the Genstat client.
%MESSAGEBOX displays text in a dialog in the Genstat client.
%OPEN open a binary file for use with %WRITE.
%FPOSITION returns the current position in the binary file opened by %OPEN.
%WRITE  writes values of data structures to a binary file opened by %OPEN.
%CLOSE closes the binary file opened by %OPEN.
%SLEEP pauses execution of the server for a time specified in seconds.
%TEMPFILE creates a unique temporary file in the Genstat temporary folder.

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. You can also execute code within an external DLL using the EXTERNAL directive and the OWN function.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. 

SUSPEND suspends the execution of Genstat to carry out operating-system commands
PASS runs another computer program, taking data from Genstat and transferring results back
SHELLEXECUTE launches executables or opens files in another application using their file extension
EXTERNAL declares an external function in a DLL for use by the OWN function
OWN executes the user’s own code linked into Genstat
BGXGENSTAT runs WinBUGS or OpenBUGS from Genstat in batch mode using scripts
RXGENSTAT submits a set of commands externally to R and reads the output


Updated on February 17, 2022

Was this article helpful?