Propeller Manual

Table Of Contents
2: Spin Language Reference – REPEAT
Propeller Manual v1.1 · Page 191
The REPEAT command automatically determines whether the range suggested by Start and
Finish is increasing or decreasing. Since the above example used 0 to 9, the range is an
increasing range; adjusting
Index by +1 every time. To get the count to go backwards,
simply reverse the Start and Finish values, as in:
repeat Index from 9 to 0 'Repeat 10 times
byte[$7000][Index]++ 'Increment RAM $7009 down through $7000
This example also loops 10 times, but counts with Index from 9 down to 0; adjusting Index
by -1 each time. The contents of the loop still increments the values in RAM, but from
locations $7009 down to $7000. After the tenth iteration,
Index will equal -1.
Since the Start and Finish fields can be expressions, they can contain variables. The next
example assumes that
S and F are variables created previously.
S := 0
F := 9
repeat 2 'Repeat twice
repeat Index from S to F 'Repeat 10 times
byte[$7000][Index]++ 'Increment RAM locations 7000..$7009
S := 9
F := 0
The above example uses a nested loop. The outer loop (the first one) repeats 2 times. The
inner loop repeats with
Index from S to F, which were previously set to 0 and 9, respectively.
The inner loop increments the values in RAM locations $7000 to $7009, in that order,
because the inner loop is counting iterations from 0 to 9. Then, the inner loop terminates
(with
Index being set to 10) and the last two lines set S to 9 and F to 0, effectively swapping
the Start and Finish values. Since this is still inside the outer loop, the outer loop then
executes its contents again (for the second time) causing the inner loop to repeat with
Index
from 9 down to 0. The inner loop increments the values in RAM locations $7009 to $7000,
in that order (reverse of the previous time) and terminates with
Index equaling -1. The last
two lines set
S and F again, but the outer loop does not repeat a third time.
REPEAT loops don’t have to be limited to incrementing or decrementing by 1 either. If the T
REPEATT command uses the optional STEP Delta syntax, it will increment or decrement the
Variable by the Delta amount. In the syntax 2 form,
REPEAT is actually always using a Delta
value, but when the “
T
STEP Delta” component is omitted, it uses either +1 or -1 by default,
depending on the range of Start and Finish. The following example includes the optional
Delta value to increment by 2.
repeat Index from 0 to 8 step 2 'Repeat 5 times