Positron Compiler Documentation

Tern

Positron8 · Revision 4.0.6.5 · PDF page 317

Variable = Tern (Param1 Condition Param2, ValueIfTrue, ValueIfFalse)

The Tern function is a form of Ternary Operator and has the same mechanism as a C compiler's '?' directive. If the comparison between Param1 and Param2 is true, then the assignment Variable will be loaded with ValueIfTrue, otherwise it will be loaded with ValueIfFalse. It is the equivalent of:

If Param1 condition Param2 Then
  Variable = ValueIfTrue
Else
  Variable = ValueIfFalse
EndIf

But it is in a single line of code and the asm code it produces is the same as it is if the comparison was written as above.

Param1 can be any valid variable, constant, expression or procedure call that will be compared to Param2. Param2 can be any valid variable, constant, expression or procedure call that will be compared to Param1. Condition is a standard set of comparison characters as found in the If command. They can be <, >, <=, >=, <> or =. ValueIfTrue can be any valid variable, constant or procedure call that will be loaded into the assignment Variable if the comparison between Param1 and Param2 is true. ValueIfFalse can be any valid variable, constant or procedure call that will be loaded into the assignment Variable if the comparison between Param1 and Param2 is false.

' Load a bit with a 1 or a 0 depending on the state of a variable
'
  Device = 18F25K20          ' Use an 18F device
  Declare Xtal = 16          ' Tell the compiler the device is operating at 16MHz
  Declare Hserial_Baud = 9600  ' Choose the Baud rate for HRsoutLn

  Dim Wordin as Word          ' Create an unsigned Word variable
  Dim Bitout as Bit          ' Create a Bit variable

  Wordin = 1024
'
' Load Bitout with 1 if Wordin holds less than 1000
'
  Bitout = Tern(Wordin < 1000, 1, 0)
  HRsoutLn "Bitout = ", Dec Bitout
'
' Load Bitout with 1 if Wordin holds greater than 1000
'
  Bitout = Tern(Wordin > 1000, 1, 0)
  HRsoutLn "Bitout = ", Dec Bitout

See also : If..Then..ElseIf..Else..EndIf.

View original PDF page 317Open page in PDFOriginal PDF page 317