Propeller Manual

Table Of Contents
REPEAT – Spin Language Reference
Simple Finite Loops (Syntax 1)
Most loops are finite in nature; they execute a limited number of iterations only. The
simplest form is syntax 1 with the Count field included.
For example:
repeat 10 'Repeat 10 times
!outa[25] 'Toggle P25
byte[$7000]++ 'Increment RAM location $7000
The above code toggles P25 ten times, then increments the value in RAM location $7000.
Note that the Count field may be any numeric expression but the expression is evaluated only
once, the first time the loop is entered. This means that any changes to the expression’s
variables within the loop will not affect the number of iterations of the loop. The next
example assumes the variable
Index was created previously.
Index := 10 'Set loop to repeat 10 times
repeat Index 'Repeat Index times
!outa[25] 'Toggle P25
Index := 20 'Change Index to 20
In the above example, Index is 10 upon entering the REPEAT loop the first time. Each time
through the loop, however,
Index is set to 20, but the loop continues to execute only 10 times.
Counted Finite Loops (Syntax 2)
Quite often it is necessary to count the loop iterations so the loop’s code can perform
differently based on that count. The
REPEAT command makes it easy to do this with syntax 2.
The next example assumes the variable
Index was created previously.
repeat Index from 0 to 9 'Repeat 10 times
byte[$7000][Index]++ 'Increment RAM locations $7000 to $7009
Like the previous example, the code above loops 10 times, but each time it adjusts the
variable
Index. The first time through the loop, Index will be 0 (as indicated by the “from 0”)
and each iteration afterwards
Index will be 1 higher than the previous (as indicated by the “to
9”): ..1, 2, 3…9. After the tenth iteration,
Index will be incremented to 10 and the loop will
terminate, causing the next code following the
REPEAT loop structure to execute, if any exists.
The code in the loop uses
Index as an offset to affect memory, byte[$7000][Index]++; in this
case it is incrementing each of the byte-sized values in RAM locations $7000 to $7009 by 1,
one at a time.
Page 190 · Propeller Manual v1.1