Propeller Manual

Table Of Contents
IF – Spin Language Reference
if X > 10 AND X < 100 'If X greater than 10 and less than 100
This IF statement would be true if, and only if, X is greater than 10 and X is also less than 100.
In other words, it’s true if
X is in the range 11 to 99. Sometimes statements like these can be
a little difficult to read. To make it easier to read, parentheses can be used to group each sub-
condition, such as with the following.
if (X > 10) AND (X < 100)'If X greater than 10 and less than 100
Using IF with ELSE
The second most common form of the
IF conditional command performs an action if a
condition is true or a different action if that condition is false. This is written as an
IF
statement followed by its IfStatement(s) block, then an
ELSE followed by its ElseStatement(s)
block, as shown below:
if X > 100 'If X is greater than 100
!outa[0] 'Toggle P0
else 'Else, X <= 100
!outa[1] 'Toggle P1
Here, if X is greater than 100, I/O pin 0 is toggled, otherwise, X must be less than or equal to
100, and I/O pin 1 is toggled. This
IF...ELSE construct, as written, always performs either a
toggle on P0 or a toggle on P1; never both, and never neither.
Remember, the code that logically belongs to the IfStatement(s) or the ElseStatement(s) must
be indented from the IF or the ELSE, respectively, by at least one space. Also note that the
ELSE must be lined up horizontally with the IF statement; they must both begin on the same
column or the compiler will not know that the
ELSE goes with that IF.
For every
IF statement, there can be zero or one ELSE component. ELSE must be the last
component in an
IF statement, appearing after any potential ELSEIFs.
Using IF with ELSEIF
The third form of the
IF conditional command performs an action if a condition is true or a
different action if that condition is false but another condition is true, etc. This is written as
an
IF statement followed by its IfStatement(s) block, then one or more ELSEIF statements
followed by their respective ElseIfStatement(s) blocks. Here’s an example:
if X > 100 'If X is greater than 100
!outa[0] 'Toggle P0
elseif X == 90 'Else If X = 90
Page 114 · Propeller Manual v1.1