Navigation bar
  Start Previous page
 79 of 172 
Next page End  

ZBasic Language Reference
72
ZX Microcontroller Family
customers, etc.  It also provides a fast way to logically remove blocks of code from your program while
leaving the source code intact so that it can be easily restored.
A key element of conditional compilation is the ability to define special identifiers and to give them values. 
These identifiers can then be used in conditional expressions that control whether or not a block of code
will be processed normally by the compiler or ignored completely.  The compiler supports the definition of
conditional identifiers on the command line but you can also define them in your source code as well
using the following syntax:
#define <identifier> [ [=] <expression> ]
Here, <identifier>  is the name of the conditional identifier that you want to define.  You may also
give it a value, represented by <expression>, which may be an integral or string type.  If you do not
specify a value, a default value of 1 is used.  The expression may include literals or identifiers previously
Examples
#define EXPERIMENTAL
#define Version 23
#define Greeting = "Hello"
If you attempt to define a conditional identifier that is already defined, you will get an error message to
that effect.  If you want to redefine a conditional identifier you must first “undefine” the existing one using
the directive:
#undef <identifier>
If the specified identifier is not actually defined, no error message will be issued so you may freely use
this directive to ensure that no definition exists prior to defining a conditional identifier.  Note that
undefining an identifier that was defined on the command line only has effect in the current module.  All
other modules will see the original value.
Once you have defined your conditional identifiers, you may use them in conditional directives that are
similar to If statements.  The first two forms presented below are complementary.
#ifdef <identifier>
<other-text>
#endif
#ifndef <identifier>
<other-text>
#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.
Previous page Top Next page