IF/THEN/ELSE/ENDIF

Format

IF condition THEN label1 <--Format 1
IF condition THEN label1 ELSE label2 <--Format 2

IF condition THEN <--Format 3

ENDIF

IF condition THEN <--Format 4

ELSE

ENDIF

Description

This statement is used to conditionally transfer to a different location in the program. The operand "condition" is one of the following:

number1 > number2          number1 >= number2
string1 > string2          string1 >= string2
number1 = number2          number1 <> number2
string1 = string2          string1 <> string2
number1 < number2          number1 <= number2
string1 < string2          string1 <= string2


Format 1--If "condition" is true, control is transferred to label1.
Otherwise the program continues at the next statement.

Format 2--If "condition" is true, control is transferred to label1,
otherwise the program branches to label2 and continues execution from there.

Format 3--If "condition" is true, the program will continue execution at the next statement. If the condition is false, the program will branch to the statement following the next ENDIF statement encountered.

Format 4--If "condition" is true, program will continue execution at the next statement up to the next ELSE statement encountered, at which point it will branch to the statement following the next ENDIF statement encountered. If "condition" is false, the program will branch to the next ELSE statement and resume execution at that statement.

Example 1

IF A > 0 THEN APLUS:
In this example, if A is greater than zero, the program jumps to the label called "APLUS:".

Example 2

IF A$="YES" THEN TRUE: ELSE FALSE:
In this example, if A$ contains the string "YES", the program jumps to the label called "TRUE:", otherwise the program transfers to label "FALSE:"

Example 3

OPEN "I",1,"TEST.DAT"
IF ERROR>0 THEN
&nbsp:&nbsp:&nbsp:&nbsp: PRINT "ERROR OPENING FILE";
&nbsp:&nbsp:&nbsp:&nbsp: PRINT ERROR
&nbsp:&nbsp:&nbsp:&nbsp: END
ENDIF
REM rest of program follows
.
.
In this example, if there is an error opening file "TEST.DAT", the program will print an error message and terminate. Otherwise control will fall through to the REM statement and the program will continue.

Example 4

PRINT "ENTER A NUMBER";
INPUT NUMBER
IF NUMBER > 100 THEN
         PRINT "THAT'S A BIG NUMBER"
ELSE
         PRINT "THAT'S A SMALL NUMBER"
ENDIF

See Also

Formats 3 and 4 are BLOCK IF formats. You can nest BLOCK IF statements up to 25 levels deep. The example below is shown three levels deep.
          IF A=1 THEN                                  <-Level 1
               IF B=2 THEN                             <-Level 2
                    IF C=3 THEN                        <-Level 3
                         PRINT "C=3, B=2, AND A=1"
                    ELSE
                         PRINT "C<>3, B=2, AND A=1"    <-ELSE  for Level 3
                    ENDIF                              <-ENDIF for Level 3
               ENDIF                                   <-ENDIF for Level 2
          ENDIF                                        <-ENDIF for Level 1
It's a good idea to indent your IFs, ELSEs and ENDIFs as shown above. Otherwise matching IFs up with matching ENDIFs and ELSEs can become very confusing.