ZBasic Language Reference
76
ZX Microcontroller Family
#endif
The first form specifies that if the given <identifier> is defined, the compiler should process the text
up to the matching #endif but if the <identifier> is not defined, the compiler should ignore the text
up to the matching #endif. The second form has the opposite effect.
Example
#ifdef EXPERIMENTAL
Call TestSetup(i)
#endif
This allows the subroutine call to be compiled into the application if EXPERIMENTAL is defined, otherwise
it is left out.
As you might have already guessed, the conditional syntax also allows an #else clause.
#ifdef EXPERIMENTAL
ver = "X006"
#else
ver = "V1.4"
#endif
An alternate form of conditional directive allows you to specify an expression involving conditional
identifiers and integer or string literals, the Boolean value of which determines whether the code within
the conditional block is processed normally or not.
#if <expression>
<other-text>
#elseif <expression>
<other-text>
#else
<other-text>
#endif
The <expression> element may be any legal ZBasic expression involving constants, conditional
identifiers, ZBasic operators and ZBasic System Library functions that can be evaluated at compile time.
The usual type-compatibility rules apply, e.g., you cannot add an integral value and a string value.
Additionally, the special operator defined() may be used in a conditional expressions. The parameter
to the defined() operator should be an identifier and the result will be non-zero or zero depending on
whether the identifier is defined or not.
Examples
#if defined(EXPERIMENTAL)
Debug.Print "Experimental version"
#endif
#if defined(Pin.RedLED)
Call PutPin(Pin.RedLED, zxOutputLow)
#endif
The #elseif clause in the conditional construction may appear zero or more times. The #else clause
may appear at most once. The <other-text> element represents arbitrary text and may contain other
conditional constructs. There is no practical limit on the nesting of conditionals.
Examples
#if Version >= 23