IF

Enables conditional execution of commands.

Syntax

IF expression1
 commands1
[ «ELSEIF expression2
 commands2»]
[ELSE
 commands3]
ENDIF

Parameters

expression1 and expression2

Logic expressions (using conditional or Boolean operators).

commands1

Commands to be executed if expression1 is logically true. Execution then resumes at the first command following ENDIF.

commands2

Commands to be executed if expression1 is false and expression2 is true. Execution then resumes at the first command following ENDIF.

commands3

Commands to be executed if expression1 and expression2 are both false. Execution then resumes at the first command following ENDIF.

ENDIF

Marks the end of the IF structure. An IF command must always have a corresponding ENDIF.
If a necessary ENDIF is missing, all subsequent commands in the current procedure are treated as part of the IF structure, and all those commands are executed only if execution control has passed to the corresponding IF, ELSEIF, or ELSE clause. When the end of the procedure is reached, the "open" IF command is closed.

Comments

An IF command must always have a corresponding ENDIF.

If expression1 evaluates to $True, commands1 are executed, then execution resumes at the first command following ENDIF. If expression1 evaluates to $False, the ELSEIF expressions (if included) are evaluated in order. Any number of ELSEIF clauses can appear between IF and ENDIF. The commands associated with the first ELSEIF expression that evaluates to "true" are executed, then execution resumes at the first command following ENDIF. If none of the expressions evaluate to "true", the commands associated with the ELSE clause (if included) are executed. If the ELSE clause is omitted, execution resumes at the first command following the ENDIF without any commands in the IF structure being executed.

If you exit from a procedure in the middle of an IF structure, the software closes the open IF command.

If you explicitly transfer control out of a procedure after an IF command has been executed, but before the corresponding ENDIF command has been executed, the open IF structure is automatically closed. Also, if the end of the current procedure is reached before a corresponding ENDIF is encountered, the open IF structure is closed.

If you forget to use a necessary ENDIF, all subsequent commands in the current procedure are treated as part of the IF structure, and all of those commands are executed only if execution control has been passed to the corresponding IF, ELSEIF, or ELSE clause.

Example

IF commands are useful for controlling groups of alternative commands. For example, the following application program fragment can be used to drive a form:

window set accelerator Return Enter Escape
while    % continue looping (until user presses Escape)
 form open menuform
 form display
 form input
 if Event.EventName = "Escape"
  return  % exits from program
   endif
 if menuform.selnum = 1
    DoEmp  % calls program DoEmp
  continue % returns to the start of the WHILE loop
   endif
 :
endwhile

ELSEIF used to test a series of alternatives.

if Age < 18
 :
elseif Age between 18 and 55
 :
elseif Age between 56 and 65
 :
else
 :
endif

Because the first ENDIF statement is missing, the second IF statement is executed only if the conditional expression for the first is false. If the second IF statement is executed, and evaluates to true, that IF statement is ended by the RETURN command; otherwise, both IF statements end when the last command in the procedure is executed.

if Age > 21
 ..first group of commands...
else
 ...second group of commands...
% endif should go here, but was missed
if Salary > 20000
 ...first group of commands...
 return
% another endif is missing here

 

Related Topics