Positron Compiler Documentation

If..Then..ElseIf..Else..EndIf

Positron16 · Revision 1.1.3.5 · PDF pages 314–315

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.

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.

Comparison is composed of variables, numbers and comparators. Instruction is the statement to be executed should the comparison fulfil the If criteria

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.

View original PDF page 314Open page in PDFOriginal PDF page 314

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.

If MyVar1 & 1 = 0 Then
  MyVar2 = 0
  MyVar3 = 1
Else
  MyVar2 = 1
EndIf
If MyVar4 = 1 Then
  MyVar2 = 0
  MyVar3 = 0
EndIf
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

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.

View original PDF page 315Open page in PDFOriginal PDF page 315