15-breaking-returning.fth 772 B

1234567891011121314151617181920212223242526272829303132
  1. \ Looping is fine, but what, if one needs to break a loop?
  2. : mydef
  3. 10 5 u+do
  4. i 2 mod 0 =
  5. if
  6. ." at index: " i . cr
  7. ." hit an even number -- oh noes!" cr
  8. \ Need to call `unloop` to not mess up everything. Loops
  9. \ have implicit variables on the stack, to represent
  10. \ their state. These variables need to be cleaned up
  11. \ before exiting.
  12. unloop
  13. \ exit leaves the current definition. So it is like
  14. \ `return` in other languages.
  15. exit
  16. then
  17. loop ;
  18. : mydef
  19. 10 5 u+do
  20. i 2 mod 0 =
  21. if
  22. ." at index: " i . cr
  23. ." hit an even number -- oh noes!" cr
  24. \ `leave` seems to do both, `unloop` and `exit`. It is a
  25. \ cleaner way of doing things, because there is no way
  26. \ to forget to unloop.
  27. leave
  28. then
  29. loop ;