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
CompilerEndIfAlso 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 hasHTML,WEB,RELEASE; the Bevy export hasBEVY,RELEASE. In an export the example keeps only the#ELSEIFor#ELSEline. - An unknown symbol is simply false. A typo in a symbol name is not an error.
- No numbers, strings, comparisons, variables or constants:
#IF 1and#IF LEVEL = 3are refused withInvalid BASIC compiler condition. A condition is settled before any variable exists. NOTbinds tighter thanAND, andANDtighter thanOR. Here&&,||and!are the logical words, not the bitwise operators of ordinary code.- The end is
#ENDIForCompilerEndIf, in one word.#END IFis not recognised and leaves the block open:#IF requires #ENDIF. - No
THEN:#IF DEBUG THENis 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 webworks. Directives insideREMSTART…REMENDor/*…*/are ignored. - PureBasic's
CompilerIf #PB_Compiler_OS = #PB_OS_Windowsis refused:CompilerIftakes 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#ENDIFIn the manual : The preprocessor