Partilhar via


#IF ... #ENDIF Preprocessor Directive

Conditionally includes source code at compile-time.

#IF nExpression1 | lExpression1 
   Commands
[#ELIF nExpression2 | #ELIF lExpression2
   Commands...
#ELIF nExpressionN | #ELIF lExpressionN
   Commands] 
[#ELSE 
   Commands]
#ENDIF

Parameters

  • #IF nExpression1 | lExpression1Commands
    nExpression1 specifies the numeric expression that is evaluated.

    • If the expression is nonzero, the commands immediately following #IF are included in the compiled code. The #IF ... #ENDIF structure is exited and the first program line following #ENDIF is then compiled.
    • If the expression is 0, the commands immediately following #IF are not included in the compiled code. Any following #ELIF directives are evaluated.

    lExpression1 specifies the logical expression that is evaluated.

    • If the expression is True (.T.), the commands immediately following #IF are included in the compiled code. The #IF ... #ENDIF structure is exited, and the first program line following #ENDIF is then compiled.

    • If the expression is False (.F.), the commands immediately following #IF are not included in the compiled code. Any following #ELIF directives are evaluated.

      Note   Do not specify system variables for nExpression1 or lExpression1. System variables are not evaluated until run time.

  • #ELIF nExpression2 | #ELIF lExpression2Commands
    ...

  • #ELIF nExpressionN | #ELIF lExpressionNCommands
    If nExpression1 is 0 or lExpression1 is False (.F.), the #ELIF directives are evaluated. The first #ELIF expression nExpression2 or lExpression2, if present, is evaluated. If nExpression2 is nonzero or lExpression2 is True (.T.), the commands following #ELIF are included in the compiled code. The #IF ... #ENDIF structure is exited, and the first program line following #ENDIF is then compiled.

    If nExpression2 is 0 or lExpression2 is False (.F.), the commands following #ELIF are not included in the compiled code. The next #ELIF directive is evaluated.

  • #ELSE Commands
    If no #ELIF directives are included, or, if those that are included evaluate to 0 or False (.F.), the presence or absence of #ELSE determines whether any additional commands are included in the compiled code:

    • If #ELSE is included, the commands following #ELSE are included in the compiled code.
    • If #ELSE is not included, none of the commands between #IF and #ENDIF are included in the compiled code. The #IF ... #ENDIF structure is exited, and compilation continues on the first program line following #ENDIF.
  • #ENDIF
    Indicates the end of the #IF statement.

Remarks

#IF ... #ENDIF can improve the readability of source code, reduce compiled program size, and, in some cases, improve performance.

When the #IF ... #ENDIF structure is compiled, successive logical or numeric expressions within the structure are evaluated. The evaluation results determine which set of Visual FoxPro commands (if any) are included in the compiled code.

Example

In the following example, the #IF ... #ENDIF structure determines which version of Visual FoxPro compiles the program and then displays the appropriate message.

#IF 'WINDOWS' $ UPPER(VERSION( ))
   ? 'This was compiled under Visual FoxPro for Windows'
#ELIF 'MAC' $ UPPER(VERSION( ))
   ? 'This was compiled under Visual FoxPro for Macintosh'
#ELIF 'UNIX' $ UPPER(VERSION( ))
   ? 'This was compiled under FoxPro for UNIX'
#ELSE 
   ? 'This was compiled under FoxPro for MS-DOS'
#ENDIF

See Also

COMPILE Command | #DEFINE ... #UNDEF Preprocessor Directive | #IFDEF | #IFNDEF ... #ENDIF Preprocessor Directive