If…Then…Else…EndIf Statement

Executes instructions based on a specified condition.

Syntax
(1) If condition Then
stmtT1
.
.
[ElseIf condition Then]
stmtT1
.
.
[Else]
stmtF1
.
.
EndIf

(2) If condition Then stmtT1 [; stmtT2...] [Else stmtF1 [; stmtF2...]]

Parameters

Condition
Any valid test condition which returns a True (any number besides “0”) or False result (returned as a “0”). (See sample conditions below)
stmtT1
Executed when the condition is True (i.e., satisfied). (Multiple statements may be put here in a blocked If...Then...Else style.)
stmtF1
Executed when the condition is False (i.e., not satisfied). (Multiple statements may be put here in a blocked If...Then...Else style.)

Description
(1) If...Then...Else executes stmtT1, etc. when the conditional statement is True. If the condition is False then stmtF1, etc. are executed. The Else portion of the If...Then...Else instruction is optional. If you omit the Else statement and the conditional statement is False, the statement following the EndIf statement will be executed. For blocked If...Then...Else statements the EndIf statement is required to close the block regardless of whether an Else is used or not.

(2) If...Then...Else can also be used in a non blocked fashion. This allows all statements for the If...Then...Else to be put on the same line. Please note that when using If...Then...Else in a non blocked fashion, the EndIf statement is not required. If the If condition specified in this line is satisfied (True), the statements between the Then and Else are executed. If the condition is not satisfied (False), the statements following Else are executed. The Else section of the If...Then...Else is not required. If there is no Else keyword then control passes on to the next statement in the program if the If condition is False.

Notes


  • Sample Conditions:

    • a = b: a is equal to b
    • a < b: b is larger than a
    • a >= b: a is greater than or equal to b
    • a <> b: a is not equal to b
    • a > b: b is smaller than a
    • a <= b: a is less than or equal to b

    Logical operations And, Or and Xor may also be used.

  • True in the Conditions:

    Constant True is 1 and the type is Boolean, so you need to be careful when using it in a comparing condition with other type variable.

    Function main
      Integer i
      i = 3
      If i = True Then
        Print "i=TRUE"
      EndIf
    Fend
    

    When you execute the program above, “i=TRUE” is displayed. The judgement of condition including the Boolean type is done with “0” or “non-0”.

    If the value of “i” is not “0”, it is considered that the condition is established and “i=TRUE” is displayed.


See Also
Else, Select...Case, Do...Loop

If/Then/Else Statement Example
[Single Line If...Then...Else]

The following example shows a simple function which checks an input to determine whether to turn a specific output on or off. This task could be a background I/O task which runs continuously.

Function main
    Do
       If Sw(0) = 1 Then On 1 Else Off 1
    Loop
Fend

[Blocked If...Then...Else]

The following example shows a simple function which checks a few inputs and prints the status of these inputs

If Sw(0) = 1 Then Print "Input0 ON" Else Print "Input0 OFF"
'
If Sw(1) = 1 Then
    If Sw(2) = 1 Then
       Print "Input1 On and Input2 ON"
    Else
       Print "Input1 On and Input2 OFF"
    EndIf
Else
    If Sw(2) = 1 Then
       Print "Input1 Off and Input2 ON"
    Else
       Print "Input1 Off and Input2 OFF"
    EndIf
EndIf

[Other Syntax Examples]

If x = 10 And y = 3 Then GoTo 50
If test <= 10 Then Print "Test Failed"
If Sw(0) = 1 Or Sw(1) = 1 Then Print "Everything OK"