Show/Hide Toolbars

XSharp

Purpose

Conditionally execute a block of statements.

Syntax

IF <lCondition> [THEN]
 <Statements>...
[ELSEIF <lCondition>]
 <Statements>...
[ELSE]
 <Statements>...
END[IF]

Arguments

[THEN]The THEN keyword is optional and has been added because Visual FoxPro allows this keyword.

 

<lCondition>If this expression evaluates to TRUE, the statements following it up until the next ELSEIF, ELSE, or ENDIF are executed.  Afterwards, control branches to the statement following the next ENDIF statement.

 

ELSEIf all preceding IF and ELSEIF conditions evaluate to FALSE, the statements following the ELSE up until the next ENDIF are executed.  Afterwards, control branches to the statement following the next ENDIF statement.

Description

IF works by branching to the statement following the first <lCondition> that evaluates to TRUE.  If all conditions evaluate to FALSE, it branches to the statement following the ELSE statement (if specified).  Execution proceeds until the next ELSEIF, ELSE, or ENDIF is encountered, and control then branches to the first statement following the next ENDIF statement.

 

Control structures can be nested to any depth.  The only requirement is that each control structure be properly nested.

 

Note: IF...ELSEIF...ENDIF is identical to DO CASE...ENDCASE, with neither syntax having a performance advantage over the other.  The IF construct is also similar to the IIf() operator which can be used within expressions.

Examples

This example evaluates a number of conditions using an IF...ELSEIF...ENDIF construct:

 

LOCAL nNumber := 0
 
IF nNumber < 50
 ? "Less than 50"
ELSEIF nNumber = 50
 ? "Is equal to 50"
ELSE
 ? "Greater than 50"
ENDIF

See Also

BEGIN SEQUENCE, DO CASE, DO WHILE, FOR, IIf()