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

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.

Equate

Definition

%PB_CC32

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

%PB_DLL16

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

%PB_DLL32

Synonym of %PB_WIN32

%PB_WIN32

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

%PB_REVISION

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

%PB_REVLETTER

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

%PB_EXE

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 %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 equates to produce a TRUE/FALSE result.  For  example:

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

  SoftwareVersion$ = "not 7.x"

#ELSE

  SoftwareVersion$ = "7.x"

#ENDIF

See also

%DEF operator, IF statementIF block

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 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