Purpose |
Define sections of source code to be compiled or ignored, depending on a certain condition. This is often referred to as conditional compilation. | ||||||||||||||||
Syntax |
#IF [NOT] {%equate | %DEF({%numeric_equate | $string_equate}) | expression} [statements] [#ELSEIF [NOT] {%equate | %DEF({%numeric_equate | $string_equate}) | expression} [statements]] [#ELSE [statements]] #ENDIF | ||||||||||||||||
Remarks |
%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.
| ||||||||||||||||
|
Examples of valid expressions can include: #IF %DEBUG = -1& #IF %DEBUG AND (NOT %RELEASE) #IF NOT %DEBUG #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
#IF (%PB_REVISION AND &H0FF00) - &H0700 SoftwareVersion$ = "not 7.x" #ELSE SoftwareVersion$ = "7.x" #ENDIF | ||||||||||||||||
See also |
|||||||||||||||||
Example |
Example ' 1. Conditional compilation by equate value %DEBUG = -1 'set to 0 for no debugging #IF %DEBUG CALL SubRoutine(Arg1, Arg2, Arg3, Answer) CALL DisplayDebugData(Answer) #ELSE CALL SubRoutine(Arg1, Arg2, Arg3, Answer) #ENDIF
' 2. Conditional compilation for EXE or DLL #IF %PB_EXE ' we are compiling to an EXE (PB/CC or PB/Win) FUNCTION PBMAIN [statements] END FUNCTION #ELSE ' we are compiling to a DLL (PB/Win) FUNCTION PBLIBMAIN [statements] END FUNCTION #ENDIF |