Introduces a block-if control structure.
A block-if structure consists of one or more alternative sets of statements. The first of these is introduced by an IF statement. There may then be further sets introduced by ELSIF statements. Then you can have a final set introduced by an ELSE statement, and the whole structure is terminated by an ENDIF statement. Thus the general form is:
then either none, one, or several blocks of statements of the form
then, if required, a block of the form
and finally the statement
Each expression must evaluate to a single number, which is treated as a logical value: a zero value is treated as false and non-zero as true. GenStat executes the block of statements following the first true expression. If none of the expressions is true, the block of statements following ELSE (if present) is executed.
You can thus use these directives to built constructs of increasing complexity. The simplest form would be to have just an IF statement, then some statements to execute, and then an ENDIF. For example:
IF MINIMUM(Sales) < 0
PRINT 'Incorrect value recorded for Sales.'
If the variate Sales contains a negative value, the PRINT statement will be executed. Otherwise GenStat goes straight to the statement after ENDIF.
To specify two alternative sets of statements, you can include an ELSE block. For example
IF Age < 20
CALCULATE Pay = Hours*1.75
CALCULATE Pay = Hours*2.5
calculates Pay using two different rates: 1.75 for Age less than 20, and 2.5 otherwise.
Finally, to have several alternative sets, you can include further sets introduced by ELSIF statements. Suppose that we want to assign values to X according to the rules:
X=1 if Y=1
X=2 if Y ≠ 1 and Z=1
X=3 if Y ≠ 1 and Z=2
X=4 if Y ≠ 1 and Z ≠ 1 or 2
This can be written in GenStat as follows:
IF Y == 1
CALCULATE X = 1
ELSIF Z == 1
CALCULATE X = 2
ELSIF Z == 2
CALCULATE X = 3
CALCULATE X = 4
If Y is equal to 1, the first CALCULATE statement is executed to set X to 1. If Y is not equal to 1, GenStat does the tests in the ELSIF statements, in turn, until it finds a true condition; if none of the conditions is true, the CALCULATE statement after ELSE is executed to set X to 4. Thus, for Y=99 and Z=1, GenStat will find that the condition in the IF statement is false. It will then test the condition in the first ELSIF statement; this produces a true result, so X is set to 2. GenStat then continues with whatever statement follows the ENDIF statement. Block-if structures can be nested to any depth, to give conditional constructs of even greater flexibility.
Directives: ELSIF, ELSE, ENDIF, EXIT, CASE, OR, ENDCASE, FOR, ENDFOR, CALCULATE.