The word each ( int/str/list/hash -- ) loops over different data types, providing the next item on the stack each loop interation.
# Integer argument = counted loops.
# Here $i will go from 0 to 9.
10 each as $i
$i print "\n" print
# String argument = per character code loops
'hello world' each as $c
$c dup chr "the next character is %s (ascii: %d)\n" format print
# List loops = item by item loop
[ 1 2 3 4 5 ] each as $value
$value print "\n" print
# Hash loops = value by value loop
{ 'a' 1 'b' 2 'c' 3 } each as $value
$value print "\n" print
The word pair ( -- index/key ) works with each to give access to loop indexes.
# String character indexes
'hello world' each pair as $i , $c
$c chr $i "character %d is %s\n" format print
# List numeric indexes
[ 1 2 3 4 5 ] each pair as $index , $value
$index "index is %d\n" format print
$value "value is %d\n" format print
# Hash string keys
{ 'a' 1 'b' 2 'c' 3 } each pair as $key , $value
$key "key is %s\n" format print
$value "value is %d\n" format print
Conditional loops use repeat ( - ) and while ( f - ) and until ( f - ).
repeat <condition> while
do_something
repeat <condition> until
do_something_else
eof