1. Home
2. IF directive

# IF directive

Introduces a block-if control structure.

### Parameter

    expression Logical expression, indicating whether or not to execute the first set of statements.

### Description

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:

first

`IF` expression

statements

then either none, one, or several blocks of statements of the form

`ELSIF` expression

statements

then, if required, a block of the form

`ELSE`

statements

and finally the statement

`ENDIF`

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.'`
`ENDIF`
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`
`ELSE`
`  CALCULATE Pay = Hours*2.5`
`ENDIF`

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`
`ELSE`
`  CALCULATE X = 4`
`ENDIF`

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.

Options: none.

Parameter: unnamed.

Directives: `ELSIF`, `ELSE`, `ENDIF`, `EXIT`, `CASE`, `CALCULATE`.

Commands for: Program control.

### Example

```" Example 1:5.3.2. 1:5.4.4 "
PROCEDURE '%TRANSFORM'
" Define the arguments of the procedure."
OPTION NAME='METHOD'; MODE=t;\
VALUES=!t(Logit,Comploglog,Angular);\
DEFAULT='LOGIT'
PARAMETER NAME='PERCENT','RESULT';\
MODE=p; SET=yes; DECLARED=yes,no;\
TYPE=!t(scalar,variate,matrix,symmetric,diagonal,table);\
COMPATIBLE=*,!t(type,nvalues);\
PRESENT=yes,no
IF METHOD .EQS. 'Logit'
CALCULATE RESULT = LOG( PERCENT / (100-PERCENT) )
ELSIF METHOD .EQS. 'Comploglog'
CALCULATE RESULT = LOG( -LOG((100-PERCENT)/100) )
ELSIF METHOD .EQS. 'Angular'
CALCULATE RESULT = ANGULAR(PERCENT)
ENDIF
ENDPROCEDURE

VARIATE    [VALUES=10,20...90] Every10%
" default setting 'logit' for METHOD "
%TRANSFORM Every10%; RESULT=Logit10%
PRINT      Every10%,Logit10%; DECIMALS=0,3
%TRANSFORM [METHOD=A] 25,50,75; RESULT=Ang25,Ang50,Ang75
PRINT      Ang25,Ang50,Ang75

COMMANDINFORMATION '%TRANSFORM','CAPTION','DOTPLOT','NOTONE';\
IMPLEMENTATION=tranimp,capimp,dotimp,notimp;\
CHANNEL=tranchan,capchan,dotchan,notchan;\
PRESENT=trancheck,capcheck,dotcheck,notcheck
PRINT tranimp,tranchan,trancheck
&     capimp,capchan,capcheck
&     dotimp,dotchan,dotcheck
&     notimp,notchan,notchec
```
Updated on June 19, 2019