IF statement

Syntax

IF expression {THEN statements [ELSE statements] | ELSE 
statements}
IF expression{THEN statements[ELSE statements] |
ELSE statements}
IF expression {THEN    statementsEND [ELSE    statementsEND] | ELSE
   statementsEND}
IF expression 
{THEN    statementsEND 
[ELSE    statementsEND] |
ELSE    statementsEND }

Description

Use the IF statement to determine program flow based on the evaluation of expression. If the value of expression is true, the THEN statements are executed. If the value of expression is false, the THEN statements are ignored and the ELSE statements are executed. If expression is the null value, expression evaluates to false. If no ELSE statements are present, program execution continues with the next executable statement.

The IF statement must contain either a THEN clause or an ELSE clause. It need not include both.

Use the ISNULL function with the IF statement when you want to test whether the value of a variable is the null value. This is the only way to test for the null value since null cannot be equal to any value, including itself. The syntax is:

IF ISNULL (expression) ...

You can write IF...THEN statements on a single line or separated onto several lines. Separating statements onto several lines can improve readability. Either way, the statements are executed identically.

You can nest IF...THEN statements. If the THEN or ELSE statements are written on more than one line, you must use an END statement as the last statement of the THEN or ELSE statements.

Conditional Compilation

You can specify the conditions under which all or part of a BASIC program is to be compiled, using a modified version of the IF statement. The syntax of the conditional compilation statement is the same as that of the IF statement except for the test expression, which must be one of the following: $TRUE, $T, $FALSE, or $F.

Example

X=10
IF X>5 THEN PRINT 'X IS GREATER THAN 5';Y=3
*
IF Y>5 THEN STOP ELSE Z=9;  PRINT 'Y IS LESS THAN 5'
*
IF Z=9 THEN PRINT 'Z EQUALS 9'
ELSE PRINT 'Z DOES NOT EQUAL 9' ;  STOP
*
IF Z=9 THEN
   GOTO 10
END ELSE
   STOP
END
*
10*
IF Y>4
   THEN
      PRINT 'Y GREATER THAN 4'
   END
   ELSE
      PRINT 'Y IS LESS THAN 4'
   END

This is the program output:

X IS GREATER THAN 5
Y IS LESS THAN 5
Z EQUALS 9
Y IS LESS THAN 4