U+do
(from compat/loops.fs, if your Forth system doesn’t
have it) takes two numbers of the stack ( u3 u4 -- )
, and then
performs the code between u+do
and loop
for u3-u4
times (or not at all, if u3-u4<0
).
You can see the stack effect design rules at work in the stack effect of the loop start words: Since the start value of the loop is more frequently constant than the end value, the start value is passed on the top-of-stack.
You can access the counter of a counted loop with i
:
There is also +do
, which expects signed numbers (important for
deciding whether to enter the loop).
Assignment: Write a definition for computing the nth Fibonacci number.
You can also use increments other than 1:
Reference: Counted Loops.