If ... Elseif ...  Else ... Endif
If ... Then ... Else

Conditionally performs a function or series of statements.

Syntax:

Note: There are several forms of the if statement:

if ... endif (structured):

if expression
 series
 of
 statements

endif

if ... else ... endif (structured):

if expression
 series
 of
 statements

else

 series
 of
 statements

endif

if...elseif...else...endif

if expression
 series
 of
 statements

elseif
expression
 series
 of
 statements

else

 series
 of
 statements

endif

if ... then (single statement):

if expression then statement

if ... then ... else ... (single statement):

if expression then statement
 else statement

Parameters:

(s) expression  a condition to be evaluated.

(s) statement  any valid WIL function or command.

(s) series of statements  multiple lines of valid WIL statements.

 

The if statement evaluates the expression following it. The expression must evaluate to an integer.

In the structured forms of the if syntax, if the expression evaluates to a non zero value (@TRUE) the series of statements after the if statement up to the first matching else or endif are executed, otherwise they are skipped. In the if ... else ... endif syntax, the series of statements after the else are executed if the result of evaluating the expression is zero (@FALSE).

In the single statement forms of the if syntax, if the expression evaluates to a non zero value (@TRUE) the statement following the then keyword is executed, otherwise it is skipped. In the if ... then ... else ... syntax, the statement following the else is executed if the result of evaluating the expression is zero (@FALSE).

The elseif conditional statement can be used in conjunction with the structured if ... else ... endif syntax.  Like the if  the elseif evaluates the expression following it and the expression must evaluate to an integer.  But unlike the if statement the elseif statement is only evaluated if a preceding if or elseif statement has evaluated to zero (@FALSE).  When an elseif statement evaluates to a non zero value (@TRUE), the series of statements after the elseif statement up to the next matching elseif, else or endif are executed. Once an if or elseif statement in a if...elseif...else...endif syntax group evaluates to a non zero value (@TRUE), no other elseif statements in the group are evaluated. The elseif statement cannot be used with standalone if and else statements.

Example:
; This example guesses a # between 1 and 1023.
Message("Think of a number", "Any number between 0 and 1023")
start = 0
stop = 1023
For i = 1 To 10
   guess = (start+stop+1) /2
   If AskYesNo("Hmmmm", StrCat("Is your number smaller than ",guess))
      stop = guess - 1
   Else
      start = guess
   EndIf
Next
guess = (start+stop+1) /2
If guess==0 || guess==1023
   Message("Hmmm", StrCat(guess," eh? Testing the limits again I assume"))
ElseIf guess==13
      Message("Hmmm", StrCat(guess," seems rather unlucky to me"))
Else
      a = guess mod 2
      If a==0 Then Message("Hmmm", StrCat("Even I can figure ",guess))
      Else Message("Hmmm", StrCat("It must be ", guess ," oddly enough"))
EndIf
Exit
See Also:

For, Select, Switch, While