From 2f670a5a965b2296431e8634938ed85ffa8f0375 Mon Sep 17 00:00:00 2001 From: Liam Cooke <14365947+ljcooke@users.noreply.github.com> Date: Thu, 14 Dec 2023 14:48:02 +0000 Subject: [forth/en] Fix expected result for one-to-12 loop (#4049) --- forth.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index ff094017..ab0c4c3b 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -106,7 +106,7 @@ myloop \ 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. -- cgit v1.2.3 From be771f6c2eacdc247ec32bd153cb7e23a689fee1 Mon Sep 17 00:00:00 2001 From: "Matthew D. Scholefield" Date: Thu, 14 Dec 2023 06:56:16 -0800 Subject: [forth/en] Clarify bounds of do and the difference between ?do vs do (#4058) * Clarify bounds of do and ?do vs do * Re-introduce square into example --------- Co-authored-by: ven --- forth.html.markdown | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index ab0c4c3b..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 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 -- cgit v1.2.3