Scripting > Using preprocessor directives > #if...#elif...#else...#endif
 
#if...#elif...#else...#endif
The #if, #elif, #else, and #endif directives can be used to specify the sections of the source file that can be executed.
Syntax
#if expression
  [statements]
[#elif expression-n
  [elifstatements-n]] ...
[#else
  [elsestatements]]
#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.
expression-n
Same as expression.
elifstatements-n
One or more statements that are executed if expression-n evaluates to True and no previous expression or expression-n has evaluated to True.
elsestatements
One or more statements that are executed if no previous expression or expression-n has evaluated to True.
#elif
Specifies a section of the source file that will be executed by the scripting engine if all the previous expressions in an #if...#elif...#else...#endif conditional statement evaluate to False.
#else
Introduces a section of the source file that will be executed by the scripting engine if all the expressions in an #if...#elif...#else...#endif conditional statement evaluate to False.
#endif
Defines the end of an #if...#elif...#else...#endif conditional statement.
Remarks
expression and expression-n can only test 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