Developer Documentation Library > Scripting > Using preprocessor directives > #if...#elif...#else...#endif
 
#if...#elif...#else...#endif
Use the #if, #elif, #else, and #endif directives to specify the sections of the source file that can be executed.
Syntax
#if <expression>
  [statements]
[#elif <expression>
  [<statements>]] ...
[#else
  [<else_statements>]]
#endif
Parameters
<expression>
Any expression that can be evaluated to return a result. The result will be coerced to True or False.
<statements>
One or more statements that are executed if <expression> evaluates to True.
<else_statements>
One or more statements that are executed if no previous <expression> evaluated to True.
#elif
A section of the source file that will be executed if all the previous expressions in a conditional statement evaluate to False.
#else
A section of the source file that will be executed by the scripting engine if all the expressions in a conditional statement evaluate to False.
#endif
Defines the end of the conditional statement.
Remarks
<expression> can test only the value of (or for the existence of) identifiers defined in a #define directive (see #define and #undef). To test for equality, use == (two equal signs). To test for inequality, use != (an exclamation point and an equal sign). You can also use || (or) and && (and) to evaluate more than one identifier in a single expression, for example:
#if x == 1 && y != z
You can also test whether an identifier has been defined. Use ! to test if an identifier has not been defined. For example:
#if x && ! (y || z)
You can also use the defined preprocessor operator to test whether an identifier has been defined. For an example, see #error.
If expression evaluates to True, the statements following the #if directive are executed. If expression evaluates to False, each expression-n (if any) is evaluated in turn. When expression-n evaluates to True, the statements following the associated #elif directive are executed. If no expression-n evaluates to True, or there are no #elif directives, the statements following the #else directive, are executed. Execution then continues with the statement following the #endif directive.
The #elif and #else directives are optional. There can be as many #elif directives as required after the #if directive, but none can appear after the #else directive.
#if directives can be nested.
Example
The following example uses the #if, #efif, and #endif directives to control the type of debugging that will be included in an mrScriptBasic script. Because the identifier MyDebugLevel has been defined with a value of 2, the value of MyValue will be written to the log:
' Settings for MyDebugLevel:
' 0 - No debugging
' 1 - Display variable values in a message box
' 2 - Write variable values to the log

#define MyDebugLevel 2

Dim MyValue
MyValue = SomeValue

#if MyDebugLevel == 1
  debug.MsgBox ( "MyValue: " + MyValue )
#elif MyDebugLevel == 2
  debug.Log ( "MyValue: " + MyValue )
#endif
See also
Using preprocessor directives