Statement IF Foundation

Executes one of several blocks of statements.

Syntax
IF <lExpression1>
   <SourceCode>...
[ELSEIF <lExpression2>]
   <SourceCode>...
[ELSE]
   <SourceCode>...
END[IF]
Parameters
<lExpression>
<lExpression> is a logical expression. If its value is .T. (true), the source code is executed up to the following ELSEIF, ELSE or ENDIF statement. The first condition is defined by the statement IF, all others by the statement ELSEIF.
ELSE
The statement ELSE specifies a block of program statements to be executed if none of the indicated conditions with IF or ELSEIF evaluated to .T. (true).
Description

IF...ENDIF is a control structure used for program navigation. A program branches to the block of program statements following the first IF or ELSEIF statement, whose condition <lExpression> evaluates to .T. (true). The program executes up to the next ELSEIF, ELSE or ENDIF statement, after which execution branches to the next executable program line following the ENDIF statement. If all of the IF or ELSEIF defined conditions evaluate to .F. (false), control branches to the lines following the ELSE statement. If ELSE is not present, control branches to the lines following the ENDIF.

The number of ELSEIF conditions is unlimited. IF...ENDIF can also be deeply nested. Although different in syntax, the control structure is similar to the DO CASE...ENDCASE structure in its behavior.

Examples
IF usage
// The example demonstrates the use of IF on a 
// relative numeric comparison of an input number with 
// pre-established values. 

#include "Inkey.ch" 

PROCEDURE Main 
   LOCAL nValue := 0 

   DO WHILE .T. 
      @  0, 0 SAY "Press Esc to quit" 
      @ 10,10 SAY "Input number" GET nValue 
      READ 

      IF nValue < -10 
           ? "The number is less than -10" 
      ELSEIF nValue < 0 
           ? "The number is negative" 
      ELSEIF nValue > 10 
           ? "The number is greater than 10" 
      ELSEIF nValue > 0 
           ? "The number is positive" 
      ELSE 
           ? "The number is 0" 
      ENDIF 

      IF LastKey() == K_ESC 
         EXIT 
      ENDIF 
   ENDDO 

RETURN 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.