Lesson 8: Loops

Toka provides a few functiosn for building various types of simple loops.

The most basic form is countedLoop, which takes the following form:

  upper-limit lower-limit quote  countedLoop

countedLoop will invoke quote the specified number of times. Upper and lower indexes can be set as desired; Toka will count up or down as necessary.

Some examples:

  10 0 [ i . ] countedLoop
  0 10 [ i . ] countedLoop

Note the use of i, the loop index. When looping via countedLoop, the loop index is set to the current cycle number. Other types of loops do not set the loop index.

The other type of loop is a whileTrue or whileFalse loop. The normal form:

  quote whileTrue
  quote whileFalse

Each time quote is invoked, a value of TRUE or FALSE should be left on the stack. The loop primitives will consume this, and either repeat the loop or end the loop until the condition is not met. whileTrue continues execution if the returned value is TRUE; whileFalse continues if the returned value is FALSE.

Some examples:

  1 [ dup . 1 + dup 101 < ] whileTrue
  101 [ dup . 1 - dup 1 < ] whileFalse
changed September 16, 2007