Conditionally executes statements by evaluating one or more conditions and executing the statements following the first condition that evaluates to true.

Syntax

IF <condition expL 1>
 [ <statements> ]

[ELSEIF <condition expL 2>
 <statements>

[ELSEIF   <condition expL 3>
 <statements>...]]

[ELSE
 [ <statements> ]]

ENDIF

<condition expL>

A logical expression that determines if the set of statements between IF and the next ELSE, ELSEIF, or ENDIF command execute. If the condition is true, the statements execute. If the condition is false, control passes to the next ELSE, ELSEIF, or ENDIF.

<statements>

One or more statements that execute depending on the value of <condition expL>.

ELSEIF <condition expL> <statements>

Specifies that when the previous IF or ELSEIF condition is false, control passes to this ELSEIF <condition expL>. As with IF, if the condition is true, only the set of statements between this ELSEIF and the next ELSEIF, ELSE, or ENDIF execute. If the condition is false, control passes to the next ELSEIF, ELSE, or ENDIF.

You can enter this option as either ELSEIF or ELSE IF. The ellipsis (...) in the syntax statement indicates that you can have multiple ELSEIF statements.

ELSE <statements>

Specifies statements to execute if all previous conditions are false.

ENDIF

A required keyword that marks the end of the IF structure.

Description

Use IF to evaluate one or more conditions and execute only the set of statements following the first condition that evaluates to true. For the first true condition, dBASE Plus executes the statements between that program line and the next ELSEIF, ELSE, or ENDIF, then skips everything else in the IF structure and executes the program line following ENDIF. If no condition is true and an associated ELSE command exists, dBASE Plus executes the set of statements after ELSE and then executes the program line following ENDIF.

Use IF...ENDIF to test one condition and IF...ELSEIF...ENDIF to test two or more conditions. If you have more than three conditions to test, consider using DO CASE instead of IF. Compare the example in this section with the example for DO CASE.

If you’re evaluating a condition to decide which value you want to assign to a variable or property, you may be able to use the IIF( ) function, which involves less duplication (you don’t have to type the target of the assignment twice).

You can nest IF statements to test multiple conditions; however, the ELSEIF option is an efficient alternative. When you use ELSEIF, you don't need to keep track of which ELSE applies to which IF, nor do you have to put in an ending ENDIF.

You can put many statements for each condition. If the number of statements in a set makes the code hard to read, consider putting them in a function and calling the function from the IF statement instead.