Skip to main content

#if

Begins a block of conditional text.

Description

This macro preprocessor directive begins a block of conditional text. It takes an ObjectScript expression as an argument, tests the truth value of the argument, and compiles a block of code if the truth value of its argument is true. The block of code concludes with a #else, #elseIf, or #endif directive.

#if <expression>
  // subsequent indented lines for specified actions

  // next preprocessor directive

where <expression> is a valid ObjectScript expression. If <expression> evaluates to a non-zero value, it is true.

For example:

 KILL ^MyColor, ^MyNumber
#define ColorDay $ZDate($H,12)
#if $$$ColorDay="Monday"
 SET ^MyColor = "Red"
 SET ^MyNumber = 1
#elseIf $$$ColorDay="Tuesday"
 SET ^MyColor = "Orange"
 SET ^MyNumber = 2
#elseIf $$$ColorDay="Wednesday"
 SET ^MyColor = "Yellow"
 SET ^MyNumber = 3
#elseIf $$$ColorDay="Thursday"
 SET ^MyColor = "Green"
 SET ^MyNumber = 4
#elseIf $$$ColorDay="Friday"
 SET ^MyColor = "Blue"
 SET ^MyNumber = 5
#else
 SET ^MyColor = "Purple"
 SET ^MyNumber = -1
#endif
 WRITE ^MyColor, ", ", ^MyNumber

This code sets the value of the ColorDay macro to the name of the day at compile time. The conditional statement that begins with #if then uses the value of ColorDay to determine how to set the value of the ^MyColor variable. This code has multiple conditions that can apply to ColorDay — one for each weekday after Monday; the code uses the #elseIf directive to check these. The fall-through case is the code that follow the #else directive. The #endif closes the conditional.

Any number of spaces may separate #if and <expression>. However, no spaces are permitted within <expression>. Anything following <expression> on the same line is considered a comment and is not parsed.

Note:

If #if appears in method code and has an argument other than a literal value of 0 or 1, the compiler generates code in subclasses (rather than invoking the method in the superclass). To avoid generating this code, test conditions for a value of 0 or 1, which results in simpler code and optimizes performance.

FeedbackOpens in a new tab