xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

IF

Executes a block of statements based on one or more conditions.

Syntax

IF <Condition1>
  <Statements>
[ ELSEIF <ConditionN>
  <Statements> ]
[ ELSE
  <Statements> ]
END[IF]

Arguments

IF <Condition1> [ELSEIF <ConditionN>]
<Condition1> to <ConditionN> are logical expressions. The statements following the first expression that yields the value .T. (true) are executed.
ELSE
If no condition results in the value .T. (true), the statements following the ELSE statement, if present, are executed.

Description

This statement executes a block of statements if the condition, related to the block, evaluates to true. If a condition evaluates to true, the following statements are executed until the next ELSEIF, ELSE or END[IF] statement is encountered.

If no condition in the entire IF...ELSEIF structure evaluates to true, control is passed to the first statement following the ELSE statement.

It is possible to include other control structures like DO WHILE and FOR within a single IF...ELSE structure.

Note:  The IF...ELSEIF...ENDIF statement is equal to the DO CASE...ENDCASE statement.

Info

See also:BEGIN SEQUENCE, DO CASE, DO WHILE, FOR, If() | IIf(), SWITCH
Category: Control structures , Statements

Example

// The example shows different forms of the IF END[IF] statement.

   PROCEDURE Main
      LOCAL nSalary := 1600

      IF .F.
         ? "This will never be executed"
      END

      IF .T.
         ? "This will always be executed"
      ENDIF

      IF nSalary < 1000
         ? "Start looking for another job!"
      ELSEIF nSalary < 1500
         ? "That's pretty good"
      ELSEIF nSalary < 2000
         ? "You should be happy"
      ELSE
         ? "You should be very happy"
      ENDIF
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe