Propeller Manual

Table Of Contents
IF – Spin Language Reference
Page 116 · Propeller Manual v1.1
We did this below:
if X > 100 'If X is greater than 100
!outa[0] 'Toggle P0
elseif X > 50 'Else If X > 50
!outa[2] 'Toggle P2
elseif X == 90 'Else If X = 90 <-- ERROR, ABOVE COND.
!outa[1] 'Toggle P1 <-- SUPERSEDES THIS AND
' THIS CODE NEVER RUNS
The above example contains an error because, while X could be equal to 90, the elseif X ==
90
statement would never be tested because the previous one, elseif X > 50, would be tested
first, and since it is true, its block is executed and no further conditions of that
IF structure
are tested. If
X were 50 or less, the last ELSEIF condition is tested, but of course, it will never
be true.
Using IF with ELSEIF and ELSE
Another form of the
IF conditional command performs one of many different actions if one
of many different conditions is true, or an alternate action if none of the previous conditions
were true. This is written as with an
IF, one or more ELSEIFs, and finally an ELSE. Here’s an
example:
if X > 100 'If X is greater than 100
!outa[0] 'Toggle P0
elseif X == 90 'Else If X = 90
!outa[1] 'Toggle P1
elseif X > 50 'Else If X > 50
!outa[2] 'Toggle P2
else 'Otherwise,
!outa[3] 'Toggle P3
This is just like the example above, except that if none of the IF or ELSEIF conditions are true,
P3 is toggled.
The ELSEIFNOT Condition
The
ELSEIFNOT condition behaves exactly like ELSEIF except that it uses negative logic; it
executes its ElseIfNotStatement(s) block only if its Condition(s) expression evaluates to
FALSE. Multiple ELSEIFNOT and ELSEIF conditions can be combined in a single IF conditional
command, in any order, between the
IF and the optional ELSE.