17.4.5 %IF-%THEN-%ELSE-%END %IF Directive
The %IF-%THEN-%ELSE-%END %IF directive lets you do
the following things conditionally:
.
Compile source text
.
Execute another compiler directive
This directive differs from all others in that it can appear
anywhere in a program where a space is allowed, except
within a quoted string.
You must include %END %IF. Otherwise, the rest of the
source program becomes part of the %THEN or %ELSE
clause. You must also include a lexical expression and some
BASIC source code.
The truth or falsity of the lexical expression determines
whether BASIC compiles the source code in the %THEN
clause or the %ELSE clause. If the lexical expression is true,
BASIC does not compile the source code in the %ELSE clause.
If the lexical expression is false, BASIC does not compile the
source code in the %THEN clause. However, BASIC does
check for lexical errors (such as illegally formed numeric
constants) in the uncompiled block of code. If an uncompiled
block of code contains a lexical error, BASIC signals an error.
Even though BASIC compiles only one block of code in an
%IF-%THEN-%ELSE-%END-%IF directive, you cannot
use the same line number in both a %THEN block and an
%ELSE block. If you specify the same line number, the first
occurrence of the line number is replaced by the second when
the program is compiled.
The following example uses the %VARIANT directive, which
returns the value set by the SET VARIANT command or
/VARIANT qualifier:
%IF (%VARIANT = 2%)
%THEN DECLARE LONG int_array(100)
%ELSE DECLARE WORD int_array(100)
%END %IF
This directive allows for two possibilities. If you compile this
program with a /VARIANT=2 qualifier, then BASIC creates
an array of longword integers. If you compile this program
with any other variant value, BASIC creates an array of
word integers.
Because %IF can appear within a program line, you can
express the same directive this way:
DECLARE %IF (%VARIANT=2%) %THEN LONG %ELSE WORD %END %IF int_array(100)
A %THEN or %ELSE clause can also contain other com-
piler directives. For example, the following program creates
the lexical constant %my_constant and assigns it a value of
8. The %IF directive evaluates the conditional expression
((%my_constant + %VARIANT) >= 10%) . If this expression is
true, BASIC executes the %THEN clause, aborting the com-
pilation and issuing an error message. If the expression is
false, BASIC prints the specified message and continues to
compile your program without aborting the compilation.
%LET %my_constant = 8%
%IF ( (%my_constant + %VARIANT) >= 10% )%THEN
%ABORT "Cannot compile with VARIANT >= 2"
%ELSE
%PRINT "Successful Compilation"
%END %IF
The compilation listing shows you which clause was actually
compiled.