Syntax

Keeps the lines of the first branch whose condition is true and drops the others, before the program is compiled. A condition tests symbols, such as the target profile.

#IF <condition>
  <lines>
[#ELSEIF <condition>]
[#ELIF <condition>]
[#ELSE]
#ENDIF
#IFDEF <symbol>
#IFNDEF <symbol>
CompilerIf <condition>
CompilerElseIf <condition>
CompilerElse
CompilerEndIf

Also written : #IFDEF, #IFNDEF, CompilerIf

Parts

<condition>Symbols combined with AND (or &&), OR (or ||), NOT (or !) and parentheses. #IF DEFINED(X) and #IF DEFINED X test one symbol. TRUE and FALSE are allowed. A symbol is true when it is defined.
<lines>Any lines. In a dropped branch they are blanked and never compiled, so they may even be text that is not BASIC.
<symbol>One symbol name. #IFDEF X is #IF X, #IFNDEF X is #IF NOT X.

Good to know

  • Symbols of each profile: the editor (Play, preview) has ENGINE, WEB, DEBUG; the HTML export has HTML, WEB, RELEASE; the Bevy export has BEVY, RELEASE. In an export the example keeps only the #ELSEIF or #ELSE line.
  • An unknown symbol is simply false. A typo in a symbol name is not an error.
  • No numbers, strings, comparisons, variables or constants: #IF 1 and #IF LEVEL = 3 are refused with Invalid BASIC compiler condition. A condition is settled before any variable exists.
  • NOT binds tighter than AND, and AND tighter than OR. Here &&, || and ! are the logical words, not the bitwise operators of ordinary code.
  • The end is #ENDIF or CompilerEndIf, in one word. #END IF is not recognised and leaves the block open: #IF requires #ENDIF.
  • No THEN: #IF DEBUG THEN is refused.
  • One directive per line. Blocks nest to any depth, but a block opens and closes in the same file.
  • A file #INCLUDEd in a dropped branch is never read, and may be missing.
  • Directives and symbols ignore case: #if web works. Directives inside REMSTART … REMEND or /* … */ are ignored.
  • PureBasic's CompilerIf #PB_Compiler_OS = #PB_OS_Windows is refused: CompilerIf takes symbols only.

Example

#IF ENGINE AND DEBUGConsole("editor build")   ' editor build#ELSEIF HTMLConsole("web export")#ELSEConsole("other target")#ENDIF#IFNDEF BEVYConsole("not Bevy")   ' not Bevy#ENDIF
In the manual : The preprocessor