If..Then..ElseIf..Else..EndIf
Syntax
If Comparison Then Instruction : { Instruction }
Or, you can use the single line form syntax:
If Comparison Then Instruction : { Instruction } : ElseIf Comparison Then Instruction : Else Instruction
Or, you can use the block form syntax:
If Comparison Then
Instruction(s)
ElseIf Comparison Then
Instruction(s)
{
ElseIf Comparison Then
Instruction(s)
}
Else
Instruction(s)
EndIf
The curly braces signify optional conditions.
Overview
Evaluates the comparison and, if it fulfils the criteria, executes expression. If comparison is not fulfilled the instruction is ignored, unless an Else directive is used, in which case the code after it is implemented until the EndIf is found.
When all the instruction are on the same line as the If-Then statement, all the instructions on the line are carried out if the condition is fulfilled.
Parameters
Comparison is composed of variables, numbers and comparators. Instruction is the statement to be executed should the comparison fulfil the If criteria
Example 1
Device = 24FJ64GA002 ' Select the device to compile for
Declare Xtal = 16
Symbol LED_Pin = PORTB.4
MyByte = 3
PinLow LED_Pin
If MyByte > 4 Then PinHigh LED_Pin : DelayMs 500 : PinLow LED_Pin
In the above example, MyByte is not greater than 4 so the If criteria isn't fulfilled. Consequently, the PinHigh LED_Pin statement is never executed leaving the state of port pin PORTB.4 low. However, if we change the value of variable Var1 to 5, then the LED will turn on for 500ms then off, because MyByte is now greater than 4, so fulfils the comparison criteria.
A second form of If, evaluates the expression and if it is true then the first block of instructions is executed. If it is false then the second block (after the Else) is executed.
The program continues after the EndIf instruction.
The Else is optional. If it is missed out then if the expression is false the program continues after the EndIf line.
Example 2
If MyVar1 & 1 = 0 Then
MyVar2 = 0
MyVar3 = 1
Else
MyVar2 = 1
EndIf
If MyVar4 = 1 Then
MyVar2 = 0
MyVar3 = 0
EndIf
Example 3
If MyVar1 = 10 Then
PinHigh LED1
ElseIf MyVar1 = 20 Then
PinHigh LED2
Else
PinHigh LED3
EndIf
A forth form of If, allows the Else or ElseIf to be placed on the same line as the If: -
If MyVar1 = 10 Then High LED1 : ElseIf MyVar1 = 20 Then High LED2 : Else : High LED3
Notice that there is no EndIf instruction. The comparison is automatically terminated by the end of line condition. So in the above example, if MyVar1 is equal to 10 then LED1 will illuminate, if MyVar1 equals 20 then LED will illuminate, otherwise, LED3 will illuminate.
The If statement allows any type of variable, register or constant to be compared. A common use for this is checking a Port bit: -
If PORTA.0 = 1 Then PinHigh LED_Pin : Else : PinLow LED_Pin
Any commands on the same line after Then will only be executed if the comparison if fulfilled: -
If MyVar1 = 1 Then PinHigh LED_Pin : DelayMs 500 : PinLow LED_Pin
Notes.
A GoTo command is optional after the Then: -
If PORTB.0 = 1 Then Label
Then always required. The Positron16 compiler relies heavily on the Then part. Therefore, if the Then part of a construct is left out of the code listing, a Syntax Error will be produced.
See also : Boolean Logic Operators, Select..Case..EndSelect.

