IF ... ENDIF Command

Conditionally executes a set of commands based on the value of a logical expression.

IF lExpression [THEN]
   Commands
[ELSE
   Commands]
ENDIF

Parameters

  • lExpression
    Specifies the logical expression that is evaluated. If lExpression evaluates to true (.T.), any commands following IF or THEN and preceding ELSE or ENDIF (whichever occurs first) are executed.
    • If lExpression is false (.F.) and ELSE is included, any commands after ELSE and before ENDIF are executed.
    • If lExpression is false (.F.) and ELSE isn't included, all commands between IF and ENDIF are ignored. In this case, program execution continues with the first command following ENDIF.

Remarks

You can nest an IF ... ENDIF block within another IF ... ENDIF block.

Comments preceded by && can be placed on the same line after IF, THEN, ELSE, and ENDIF. These comments are ignored during compilation and program execution.

Example

CLOSE DATABASES
OPEN DATABASE (HOME(2) + 'Data\testdata')
USE Customer     && Open customer table

GETEXPR 'Enter condition to locate ' TO gcTemp;
   TYPE 'L' DEFAULT 'COMPANY = ""'
LOCATE FOR &gcTemp  && Enter LOCATE expression
IF FOUND( )  && Was it found?
   DISPLAY  && If so, display the record
ELSE  && If not found
   ? 'Condition ' + gcTemp + ' was not found '   && Display a message
ENDIF
USE

See Also

DO CASE ... ENDCASE | DO WHILE ... ENDDO | FOR EACH ... ENDFOR | FOR ... ENDFOR | IIF( ) | SCAN ... ENDSCAN