Propeller Manual

Table Of Contents
2: Spin Language Reference – REPEAT
Propeller Manual v1.1 · Page 189
Condition(s) is one or more Boolean expression(s) used by syntax 3 and 4 to continue
or terminate the loop. When preceded by
UNTIL, Condition(s) terminates the loop
when true. When preceded by
WHILE, Conditions(s) terminates the loop when FALSE.
Explanation
REPEAT is the very flexible looping structure for Spin code. It can be used to create any type
of loop, including: infinite, finite, with/without loop counter, and conditional zero-to-
many/one-to-many loops.
T
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.
Infinite Loops (Syntax 1)
Truthfully, any of the four forms of
REPEAT can be made into infinite loops, but the form used
most often for this purpose is syntax 1 without the Count field. For example:
repeat 'Repeat endlessly
!outa[25] 'Toggle P25
waitcnt(2_000 + cnt) 'Pause for 2,000 cycles
This code repeats the !outa[25] and waitcnt(2_000 + cnt) lines endlessly. Both lines are
indented from the
REPEAT so they belong to the REPEAT loop.
Since Statement(s) is really an optional part of
REPEAT, the REPEAT command by itself can be
used as an endless loop that does nothing but keep the cog active. This can be intentional, but
sometimes is unintentional due to improper indention. For example:
T
repeat 'Repeat endlessly
!outa[25] 'Toggle P25 <-- This is never run
The above example is erroneous; the last line is never executed because the REPEAT above it is
an endless loop that has no Statement(s); there is nothing indented immediately below it, so
the cog simply sits in an endless loop at the
REPEAT line that does nothing but keep the cog
active and consuming power.