8

I'd like to write a while() loop in Gforth. Unfortunately, the only tutorial online isn't useful due to a lack of examples, and examples on counted loops (what I'm not looking for) appear fundamentally different.

What are some concrete examples of how to represent something like this?

while (x > 3) { print(x); x--; }

Or really, just some concrete way to represent anything of the form:

while (predicate) { expression(s) }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Phillip Carter
  • 4,895
  • 16
  • 26

1 Answers1

11

Your first piece of code translates to:

\ Assuming x is on the top of the stack.
begin dup 3 > while dup . 1- repeat

\ Or if x is in memory.
begin x @ 3 > while x ? -1 x +! repeat

And the second:

begin predicate while expressions repeat
Lars Brinkhoff
  • 13,542
  • 2
  • 28
  • 48