Propeller Manual

Table Of Contents
REPEAT – Spin Language Reference
Page 192 · Propeller Manual v1.1
byte[$7000][Index]++ 'Increment even RAM $7000 to $7008
Here, REPEAT loops five times, with Index set to 0, 2, 4, 6, and 8, respectively. This code
effectively increments every other RAM location (the even numbered locations) from $7000
to $7008 and terminates with
Index equaling 10.
The Delta field can be positive or negative, regardless of the natural ascending/descending
range of the Start and Finish values, and can even be adjusted within the loop to achieve
interesting effects. For example, assuming Index and D are previously defined variables, the
following code sets
Index to the following sequence: 5, 6, 6, 5, 3.
D := 2
repeat Index from 5 to 10 step D
--D
This loop started out with Index at 5 and a Delta (D) of +2. But each iteration of the loop
decrements
D by one, so at the end of iteration 1, Index = 5 and D = +1. Iteration 2 has Index
= 6 and
D = 0. Iteration 3 has Index = 6 and D = -1. Iteration 4 has Index = 5 and D = -2.
Iteration 5 has
Index = 3 and D = -3. The loop then terminates because Index plus Delta (3 + -
3) is outside the range of Start to Finish (5 to 10).
Conditional Loops (Syntax 3 and 4)
The final forms of
REPEAT, syntax 3 and 4, are finite loops with conditional exits and have
flexible options allowing for the use of either positive or negative logic and the creation of
zero-to-many or one-to-many iteration loops. These two forms of
T
REPEATT are usually referred
to as “repeat while” or “repeat until” loops.
Let’s look at the
REPEAT form described by syntax 3. It consists of the T REPEAT command
followed immediately by either
WHILE or UNTIL then Condition(s) and finally, on the lines
below it, optional Statement(s). Since this form tests Condition(s) at the start of every
iteration, it creates a zero-to-many loop; the Statement(s) block will execute zero or more
times, depending on the Condition(s). For example, assume that
X is a variable created
earlier:
X := 0
repeat while X < 10 'Repeat while X is less than 10
byte[$7000][X] := 0 'Increment RAM value
X++ 'Increment X
This example first sets X to 0, then repeats the loop while X is less than 10. The code inside
the loop clears RAM locations based on
X (starting at location $7000) and increments X.