How to write a while() loop in Gforth
Asked Answered
V

1

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) }
Vinificator answered 4/3, 2015 at 22:22 Comment(0)
M
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
Moreville answered 5/3, 2015 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.