Propeller Manual

Table Of Contents
2: Spin Language Reference – IF
Indention is Critical
IMPORTANT: Indention is critical. The Spin language relies on indention (of one space or
more) on lines following conditional commands to determine if they belong to that command
or not. To have the Propeller Tool indicate these logically grouped blocks of code on-screen,
you can press Ctrl + I to turn on block-group indicators. Pressing Ctrl + I again will disable
that feature. See the Propeller Tool Help for a complete list of shortcut keys.
Simple IF Statement
The most common form of the
IF conditional command performs an action if, and only if, a
condition is true. This is written as an
IF statement followed by one or more indented lines of
code. For example:
if X > 10 'If X is greater than 10
!outa[0] 'Toggle P0
!outa[1] 'Toggle P1
This example tests if X is greater than 10; if it is, I/O pin 0 is toggled. Whether or not the IF
condition was true, I/O pin P1 is toggled next.
Since the
!outa[0] line is indented from the IF line, it belongs to the IfStatement(s) block
and is executed only if the
IF condition is true. The next line, !outa[1], is not indented from
the
IF line, so it is executed next whether or not the IF’s Condition(s) was true. Here’s
another version of the same example:
if X > 10 'If X is greater than 10
!outa[0] 'Toggle P0
!outa[1] 'Toggle P1
waitcnt(2_000 + cnt) 'Wait for 2,000 cycles
This example is very similar to the first, except there are now two lines of code indented from
the
IF statement. In this case, if X is greater than 10, P0 is toggled then P1 is toggled and
finally the
waitcnt line is executed. If, however, X was not greater than 10, the !outa[0] and
!outa[1] lines are skipped (since they are indented and part of the IfStatement(s) block) and
the
waitcnt line is executed (since it is not indented; it is not part of the IfStatement(s) block).
Combining Conditions
The Condition(s) field is evaluated as one single Boolean condition, but it can be made up of
more than one Boolean expression by combining them with the
AND and OR operators; see
pages 167-168. For example:
Propeller Manual v1.1 · Page 113