# IF, ELSE_IF, ELSE, END_IF: Conditional (IF) Flow Control

VISTA procedures allow testing of variables and branching based on the results of those tests. This capability greatly expands the usefulness of procedures.

The simplest use of IF is to mark a section of a procedure that is executed only if come condition is true. It has the form:

```   IF condition
Procedure lines (any number) that are executed if the
specified condition is met.
END_IF
```

You can also have two level branching:

```   IF condition
Procedure lines to be executed if the condition is true.
ELSE
Procedure lines to be executed if the condition is false.
END_IF
```

These may be strung together:

```   IF condition_1
Procedure lines to be executed if condition_1 is true.
ELSE_IF condition_2
Procedure lines to be executed when condition_1 is
false and condition_2 true.
...
ELSE_IF condition_N
Procedure lines to be executed when all conditions
are false except condition_N.
ELSE
Procedure lines to be executed if and only if
all other conditions are false.
END_IF
```

The conditions tested by the IF and ELSE_IF statements are really just VISTA arithmetic expressions. An expression is considered to be true if it evaluates to be non-zero, and false otherwise. VISTA arithmetic supports various logical operators whose value is either 1 or 0 depending on whether the logical test is true or false. The logical operators are the following, where A and B can represent single VISTA variables or algebraic expressions.

There are two logical conjunctions & (and) and | (or) which can be used to join several of the above tests. Examples of the conjunctions are below:

The syntax of the IF statements is designed to look similar to the FORTRAN-77 IF block structures. Each IF block must begin with an IF command and end with the END_IF command. An algebraic statement to be tested must follow the IF on the same line. If the relation is true, then the procedure commands following the IF command are executed. If the relation is false, VISTA looks for any ELSE_IF tests, any final ELSE statement, or jumps to the procedure lines following the END_IF statement.

The ELSE_IF command also must have a condition to be tested on the same line. ELSE_IF's are optional, but permit you to test other conditions and execute other blocks of the procedure buffer in the event that the initial IF or any preceding ELSE_IF's are false. In this way you can allow VISTA to 'trickle down' through several tests looking for one that is true.

The ELSE statement is also optional and marks a set of procedure lines for VISTA to execute if and only if the initial IF and any following ELSE_IF's all test out false. Basically, the IF, any ELSE_IF's, or any ELSE statements all mark out various blocks of the procedure to be executed under different conditions. After the execution of any block, VISTA transfers control to the procedure lines following the END_IF statement.

IF blocks can be nested within other IF blocks up to 15 levels deep. IF blocks can be jumped out of, but not into, by the GOTO command. IF blocks must contain or be contained within DO loops completely. Some examples of IF blocks are given below:

1. Simple Single Test:
```    IF X>Y
Do these procedure lines if X is greater than Y
END_IF
```

2. IF/ELSE Test:
```    IF (X>Y)&(X<Z)
Do these procedure lines if X is greater than Y but less than Z.
ELSE
END_IF
```

3. IF/ELSE_IF Test:
```    IF SKY-LIMIT>BACKGRND
Do these procedure lines if IF test true.
ELSE_IF BACKGRND==0
Do these procedure lines if IF test is false,
but the ELSE_IF condition is true.
END_IF
```

4. Test on 1 or 0 for an expression:
```    IF IMAGE-1
Do these procedure lines if IMAGE is not equal
to 1 (which would make the expression evaluate to 0)
END_IF
```