#IF #ELSE #ELSEIF #ENDIF , VAREXIST

Top  Previous  Next

Action

Conditional compilation directives intended for conditional compilation.

 

 

Syntax

#IF condition

 

#ELSEIF condition

 

#ELSE

 

#ENDIF

 

 

Remarks

Conditional compilation is supported by the compiler.

What is conditional compilation?

Conditional compilation will only compile parts of your code that meet the criteria of the condition.

 

By default all your code is compiled.

 

Conditional compilation needs a constant to test.

So before a condition can be tested you need to define a constant.

 

CONST test 1

#IF TEST

    Print "This will be compiled"

#ELSE

    Print "And this not"

#ENDIF

 

notice Note that there is no THEN and that you need to use #ENDIF which has no space between END and IF , so  #END IF is wrong!

 

You can nest the conditions and the use of #ELSE and #ELSEIF is optional.

 

There are a few internal constants that you can use. These are generated by the compiler:

_CHIP = 0

_RAMSIZE = 128

_ERAMSIZE = 128

_SIM = 0

_XTAL = 4000000

_BUILD = 11162

 

_CHIP is an integer that specifies the chip, in this case the 2313

_RAMSIZE is the size of the SRAM

_ERAMSIZE is the size of the EEPROM

_SIM is set to 1 when the $SIM directive is used

_XTAL contains the value of the specified crystal

_BUILD is the build number of the compiler.

 

The build number can be used to write support for statements that are not available in a certain version :

#IF _BUILD >= 11162

   s = Log(1.1)

#ELSE

  Print "Sorry, implemented in 1.11.6.2"

#ENDIF

 

Conditional compilation allows you to create different versions of your program but that you keep one source file.

For example you could make a multi lingual program like this :

 

CONST LANGUAGE=1

 

'program goes here

 

#IF LANGUAGE=1

  DATA "Hello"

#ENDIF

#IF LANGUAGE=2

  DATA "Guten tag"

#ENDIF

 

By changing just one constant you can have for example English or German data lines.

 

Conditional compilation does work with the $REGFILE directive but you need to set the option 'Use New Method' in Environment IDE options.

 

 

VAREXIST

A special check was added to 1.11.8.1 to test for existence of dimmed/defined constants or variables.

#IF varexist("S")

       ' the variable S was dimensioned so we can use it here

#ELSE

            ' when it was not dimmed and we do need it, we can do it here

    DIM S as BYTE

#ENDIF

 

The Editor can show non included code with a different font color. This makes it more clear which code is included.

 

 

See Also

CONST , Edit Show Excluded Code