summaryrefslogtreecommitdiffhomepage
path: root/forth.html.markdown
diff options
context:
space:
mode:
authorBoris Verkhovskiy <boris.verk@gmail.com>2024-04-03 02:02:49 -0700
committerGitHub <noreply@github.com>2024-04-03 02:02:49 -0700
commit616e40816dfdf90c2418477327729169db0274b3 (patch)
tree0b5ae76e529c34f6648442815f535626eaeebf69 /forth.html.markdown
parent3e22775a641831a82c59a3b6197240b2fcd9a76b (diff)
parent638494ae2a0c089717f2bfff965da52e3b15a4f9 (diff)
Merge branch 'master' into elixir-casing
Diffstat (limited to 'forth.html.markdown')
-rw-r--r--forth.html.markdown17
1 files changed, 9 insertions, 8 deletions
diff --git a/forth.html.markdown b/forth.html.markdown
index ff094017..4e2f7599 100644
--- a/forth.html.markdown
+++ b/forth.html.markdown
@@ -93,8 +93,8 @@ see square \ : square dup * ; ok
\ ------------------------------------ Loops -----------------------------------
-\ `do` is also a compile-only word.
-: myloop ( -- ) 5 0 do cr ." Hello!" loop ; \ ok
+\ `?do` is also a compile-only word.
+: myloop ( -- ) 5 0 ?do cr ." Hello!" loop ; \ ok
myloop
\ Hello!
\ Hello!
@@ -102,16 +102,17 @@ myloop
\ Hello!
\ Hello! ok
-\ `do` expects two numbers on the stack: the end number and the start number.
+\ `?do` expects two numbers on the stack: the end number (exclusive) and the
+\ start number (inclusive).
\ We can get the value of the index as we loop with `i`:
: one-to-12 ( -- ) 12 0 do i . loop ; \ ok
-one-to-12 \ 0 1 2 3 4 5 6 7 8 9 10 11 12 ok
+one-to-12 \ 0 1 2 3 4 5 6 7 8 9 10 11 ok
-\ `?do` works similarly, except it will skip the loop if the end and start
-\ numbers are equal.
-: squares ( n -- ) 0 ?do i square . loop ; \ ok
-10 squares \ 0 1 4 9 16 25 36 49 64 81 ok
+\ `do` works similarly, except if start and end are exactly the same it will
+\ loop forever (until arithmetic underflow).
+: loop-forever 1 1 do i square . loop ; \ ok
+loop-forever \ 1 4 9 16 25 36 49 64 81 100 ...
\ Change the "step" with `+loop`:
: threes ( n n -- ) ?do i . 3 +loop ; \ ok