#IF/#ELSEIF/#ELSE/#ENDIF metastatements


Define sections of source code to be compiled or ignored, depending on a certain condition.  This is often referred to as conditional compilation.


#IF [NOT] {%equate | %DEF({%numeric_equate | $string_equate}) | expression}


[#ELSEIF [NOT] {%equate | %DEF({%numeric_equate | $string_equate}) | expression}






%equate is a named constant or constant value.  The %DEF operator allows you to test whether an equate has been defined.  %DEF returns TRUE or FALSE.  Typical usage: #IF %DEF(%PB_DLL16)  or  #ELSEIF NOT %DEF(%PB_WIN32).  expression may be a simple numeric expression using the arithmetic operators +, -, *, /, and \, and the relational operators >, <, >=, <=, <>, and =, and may also include the CVQ function.

PowerBASIC automatically defines the equates in the following table according to the PowerBASIC compiler being used.  Please note the references to other PowerBASIC compilers are included for those writing programs that may be compilable by more than one PowerBASIC compiler.




Pre-defined as TRUE (non-zero) in PB/CC for Windows, but is not defined in other compilers.


Pre-defined as TRUE (non-zero) in PB/DLL 16-bit, FALSE (zero) in other compilers.


Synonym of %PB_WIN32


Pre-defined as TRUE (non-zero) in PB/Win 32-bit, but is not defined in other compilers.


Pre-defined as the hex revision (6.00 = &H600).


Pre-defined as the ASCII code of the revision letter (a = &H61), or &H20 if there is no revision letter.


Pre-defined as TRUE (non-zero) if compiling to EXE or as FALSE (zero) if compiling to DLL format (PB/Win only). The equate %PB_EXE is always defined in PowerBASIC, so %DEF(%PB_EXE) will always be evaluated as TRUE.  The difference being the value assigned to the equate by the compiler.


Examples of valid expressions can include:

#IF %DEBUG = -1&



#IF %VERSION <> CVQ("DemoMode")

Note that the AND, OR and NOT operators work as bitwise operators, rather than logical operators, in #IF metastatements.

If the value of %equate or if %DEF(%equate|$equate) is TRUE (non-zero) or if the result of expression is TRUE, the statements between #IF and #ELSE or #ELSEIF are compiled, and the statements between #ELSE or #ELSEIF and #ENDIF are ignored.

If the value of %equate or %DEF(%equate|$equate) is FALSE (zero) or the result of expression is FALSE, the statements between #IF and #ELSE or #ELSEIF are ignored, and those between #ELSE or #ELSEIF and #ENDIF are compiled.

The #ELSE or #ELSEIF clause and associated statements are optional, but #ENDIF is required.

Conditional compilation statements can be nested up to 16 levels deep. A primary use of conditional compilation is to include test code in your programs that will be compiled during program development (but not in the final product), and to facilitate building special editions of an application from a single source code file.

It is possible to perform bitwise operations on equates to produce a TRUE/FALSE result.  For  example:

#IF (%PB_REVISION AND &H0FF00) - &H0700

  SoftwareVersion$ = "not 7.x"


  SoftwareVersion$ = "7.x"


See also

%DEF operator, IF statementIF block


Example ' 1. Conditional compilation by equate value

%DEBUG = -1       'set to 0 for no debugging


  CALL SubRoutine(Arg1, Arg2, Arg3, Answer)

  CALL DisplayDebugData(Answer)


  CALL SubRoutine(Arg1, Arg2, Arg3, Answer)



' 2. Conditional compilation for EXE DLL


  ' we are compiling to an EXE (PB/CC or PB/Win)





  ' we are compiling to a DLL (PB/Win)