# Conditional Compilation

{PBASIC 2.5}

In order to facilitate the creation of programs that can be compiled for and downloaded to the connected model of BASIC Stamp2, several conditional compile directives have been added to PBASIC 2.5: #DEFINE, #IF...#THEN...#ELSE, #SELECT...#CASE, and #ERROR. Conditional compile directives are evaluated before the program is compiled, so variables and named constants cannot be referenced within a conditional compilation definition.

## #DEFINE

Syntax: #DEFINE Symbol {= Value}

#DEFINE allows the programmer to create custom symbols for use within conditional compilation control structures.

- Symbol is a string (up to 32 characters).
- Value is an optional constant/expression specifying the value of the defined symbol. If the value parameter is omitted, Symbol is defined as True.

Example:

    #DEFINE DebugMode = 1

In the example above, the "= 1" is optional. Note that when the compiler encounters an undefined symbol it will evaluate that symbol as False (0). You can define a symbol as False by removing the definition with a comment character ( ' ) or change its value to zero (recommended)

## #IF...#THEN...#ELSE

Syntax:
#IF Condition #THEN
Statement(s)
{ #ELSE
Statement(s) }
#ENDIF

## Function

Evaluate Condition and, if it is True, compile the statement(s) following #THEN, otherwise compile the statements following #ELSE.

- Condition is a statement, such that can be evaluated as True or False.
- Statement is any valid PBASIC statement.

Example:

    ' set Baud for 9600-N81

    #IF ($STAMP = BS2sx) OR ($STAMP = BS2p) #THEN
      Baud CON 240
    #ELSE
      Baud CON 84
    #ENDIF

In this example, the constant Baud is set to match the connected BASIC Stamp. This code will work with the BS2, BS2e, BS2sx, BS2p and BS2pe.

## #SELECT...#CASE

Syntax:
#SELECT Expression
#CASE Condition(s)
Statement(s)
{ #CASE Condition(s)
Statement(s)
#CASE #ELSE
Statement(s) }
#ENDSELECT

## Function

Evaluate Expression and then conditionally compile a block of code based on comparison to Condition(s). If no conditions are found True and a #CASE ELSE block is included, the #CASE #ELSE code statements will compiled.

- Expression is a variable, a constant or an expression.
- Condition is a statement, that can be evaluated as True or False. Multiple conditions within the same #CASE can be separated by commas ( , ).
- Statement(s) is any valid PBASIC statement or statements.

Example:

    #SELECT $STAMP
      #CASE BS2, BS2e, BS2sx
        GOSUB LCD_Write
      #CASE #ELSE
        LCDOUT LCDpin, cmd, [char]
    #ENDSELECT

## #ERROR

Syntax: #ERROR Message

#ERROR allows the programmer to create custom a custom error dialog.

- Message is the error message string.

Example:

    #DEFINE I2CReady = (($STAMP = BS2p) OR ($STAMP = BS2pe))
    #IF NOT I2CReady #THEN
      #ERROR "BS2p or BS2pe is required for this program."
    #ENDIF

The example above will halt the program and display the dialog below if you attempt to compile the program and run it on a BASIC Stamp other than the BS2p or BS2pe:

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012