QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

$IF is precompiler Metacommand, which determines which sections of code inside its blocks are included into the final code for compliing.

Syntax

$IF variable = expression THEN . $ELSEIF variable = expression THEN . $ELSE . $END IF

Example(s)


$LET ScreenMode = 32
$IF ScreenMode = 0 THEN
    CONST Red = 4
$ELSEIF ScreenMode = 32 THEN
    CONST Red = _RGB32(255,0,0)
$END IF

COLOR Red
PRINT "Hello World"

Explanation: The same CONST is defined twice inside the program. Normally, defining a CONST more than once generates an error, but the $IF condition here is choosing which CONST will be inside the final program.

As long as Screenmode is 0, the program will exclude the code where CONST Red is defined as color 4. If Screenmode is 32, CONST Red will be defined as _RGB32(255, 0, 0).

The $LET and $IF statements let the programmer control the code that actually gets compiled, while excluding the other blocks completely.


$IF WIN THEN
    CONST Slash = "\"
$ELSE
    CONST Slash = "/"
$END IF

PRINT "The proper slash for your operating system is "; Slash

Explanation: For the above, the CONST slash is defined by the automatic internal flags which returns what operating system is being used at compile time. On a Windows PC, the Slash will be the backslash; for any other OS it will be the forward slash.


$IF VERSION < 1.5 THEN
    $ERROR Requires QB64 version 1.5 or greater
$END IF

Explanation: VERSION is a predefined variable that holds the QB64 compiler version. If we know our program needs features only available above a certain version, we can check for that and give the user a helpful error message instead of a confusing error elsewhere in the program.

See Also