next up previous contents
Next: Magnitudes and Numbers Up: Control-flow Previous: Basic Conditional Evaluation   Contents


Looping

Slate includes various idioms for constructing basic loops.

n timesRepeat: block
executes the block n times.
condition whileTrue: block
and condition whileFalse: block execute their blocks repeatedly, checking the condition before each iteration.
whileTrue
and whileFalse execute their blocks repeatedly, checking the return value before repeating iterations.
a upTo: b do: block
and b downTo: a do: block executes the block with each number in turn from a to b, inclusive.
upTo:by:do:
and downTo:by:do: executes the block with each number in turn in the inclusive range, with the given stepping increment.
a below: b do: block
and b above: a do: block act identically to the previous method except that they stop just before the last value. This assists in iterating over array ranges, where the 0-based indexing makes a difference in range addresses by one, avoiding excessive use of size - 1 calls.
Slate's looping control structures can easily be extended without concern due to the fact that the interpreter unrolls properly tail-recursive blocks into low-level loop code that re-uses the same activation frame. So basically structuring custom looping code so that it calls itself last within its own body and returns that value will avoid the need for increasing stack space per iteration.


next up previous contents
Next: Magnitudes and Numbers Up: Control-flow Previous: Basic Conditional Evaluation   Contents
Brian Rice 2004-08-24