Exits from a control structure.
Sometimes you may want simply to abandon part of a program: you may be unable to do any further calculations or analyses. For example, if you are examining several subsets of the units, you would wish to abandon the analysis of any subset that turned out to contain no observations. Another example would be if you wanted to abandon the execution of a procedure whenever an error diagnostic has appeared. The EXIT directive allows you to exit from any control structure.
In its simplest form EXIT has no parameter setting, and the exit is unconditional: GenStat will always exit from the control structure or structures concerned. You are most likely to use this as part of an ELSE block of a block-if or multiple-selection structure. For example
CALCULATE Percent = R * 100 / N
PRINT [IPRINT=*] 'Incorrect value ',N,' for N.'
prints an appropriate warning message for a zero or negative value of N, and then exits from a procedure.
If the warning message is simply a text or string, the EXPLANATION option can be used to print it on exit. For example
EXIT [CONTROL=procedure; \
EXPLANATION='Incorrect value for N.'] N.LE.0
CALCULATE Percent = R * 100 / N
has the same effect except that the actual value of N is no longer printed.
The CONTROL option specifies the type of control structure from which to exit. The default setting is for, causing an exit from a FOR loop. For the other settings: if causes an exit from a block-if structure (as introduced by the IF directive), case exits from a multiple-selection structure (as introduced by CASE), procedure exits from a procedure (see the PROCEDURE directive), and job causes the entire job to be abandoned (see JOB). Sometimes, to exit from one type of control structure, others must be left too. To exit from the procedure in the above example, requires GenStat to exit also from the block-if structure. Generally, GenStat does these nested exits automatically, as required. However, inside a procedure, you can exit only from FOR loops and block-if or multiple-selection structures that are within the procedure. You cannot put, for example,
within a part of the procedure where there is no block-if in operation, and then expect GenStat to exit both from the procedure and from a block-if structure in the outer program from which the procedure was called. GenStat regards a procedure as a self-contained piece of program.
The NTIMES option indicates how many control structures of the specified type to exit from. If you ask GenStat to exit from more structures than are currently in operation in your program, it will exit from as many as it can and then print a warning. If NTIMES is set to zero or to missing value no exit takes place. If NTIMES is set to a negative value, say -n, the exit is to the end of the nth structure of the specified type, counting them in the order in which their execution began. Consider this example:
"contents of the inner loop, including:"
"amongst other statements"
ENDFOR "end of the loop over D"
ENDFOR "end of the loop over C"
ENDFOR "end of the loop over B"
ENDFOR "end of the loop over A"
If the scalar Nexit has the value 2, the exit is to the end of the loop over C; so the two exits are from the loop over D and the loop over C. But if Nexit has the value -2 the exit is to the end of the loop over B, as this is the second loop to have been started.
A further possibility when EXIT is used within a FOR loop is that you can choose either to go right out of the loop and continue by executing the statement immediately after the ENDFOR statement, or to go to ENDFOR and then repeat the loop with the next set of parameter values. To repeat the loop, you need to set option REPEAT=yes. For example, suppose that variates Height and Weight contain information about children of various ages, ranging from five to 11. The RESTRICT statement causes the subsequent GRAPH statement to plot only those units of Height and Weight where the variate Age equals Ageval. The EXIT statement ensures that the graph is not plotted if there are no units of a particular age; the program then continues with Ageval taking the next value in the list.
RESTRICT Height,Weight; CONDITION=Age.EQ.Ageval
EXIT [REPEAT=yes] NVALUES(Height).EQ.0
GRAPH Height; X=Weight
The REPEAT option can also be used within a procedures to ask GenStat to call the procedure with the next set of parameter settings.
The example of the heights and weights of children also illustrates the use of the parameter of EXIT, to make the effect conditional. The parameter is an expression which must evaluate to a single number which GenStat interprets as a logical value. If the value is zero, the condition is false and no exit takes place; for other values the condition is true and the exit takes effect as specified. This is particularly useful for controlling the convergence of iterative processes: for example
CALCULATE Clim = X/10000
CALCULATE Previous = Root
& Root = (X/Previous + Previous)/2
PRINT Root,Previous; DECIMALS=4
EXIT ABS(Previous-Root) < Clim
will calculate the square root of X to four significant figures.
Options: NTIMES, CONTROL, REPEAT, EXPLANATION.
Directives: FOR, ENDFOR, CASE, OR, ENDCASE, IF, ELSIF, ENDIF, ELSE, FAULT, CALCULATE.