Loops
Loops may be created using the Forth words for and next. The loop count is placed on the stack before calling for. The following simple example prints out five new lines:
: fivelines ( -- , prints out 5 new lines )
5 for
cr
next
;
Note that in most Forths, do…loop requires two parameters, while in FlashForth for…next just uses a single count.
Experienced Forth programmers take note!
Experienced Forth programmers take note!
for takes the operand on the top of the stack and uses this for the loop count. In the previous example, the loop executes the word cr five times. The operand can just as easily be supplied by other Forth words, or values already on the stack.
In the following example, the number of times to execute the loop is left to the user at runtime:
In the following example, the number of times to execute the loop is left to the user at runtime:
: stars ( N -- , prints N asterisks to the console )
for
42 emit
next
;
We can then use our word to output 10 asterisks:
10 stars
The loop count is accessible inside the loop using the Forth word i. Note that i counts down to zero. In this example, we print out the loop count as the loop executes:
: countdown
cr
10 for
i . cr
next
;
Conditional Loops
Conditional loops are created using the Forth words begin and until. until checks for a true boolean (anything non zero) on top of the stack. If true is found, the loop terminates, otherwise execution branches back to the word following begin.
The following trivial example waits until the Escape key (ASCII code 27) is pressed:
The following trivial example waits until the Escape key (ASCII code 27) is pressed:
: wait_for_ESC
begin
key
27 =
until
;
The equal operator (=) takes the top two values from the stack (in this case, the ASCII code obtained using key and the value 27) and compares them. If they are equal, it places a true (-1) on the stack, otherwise a false (0) is placed on the stack. (The Forth operator = is equivalent to == in C.)
In this example, we blink the LED until any key is pressed:
In this example, we blink the LED until any key is pressed:
: blinky
begin
blink
key?
until
;
An alternative conditional loop construct is begin condition while some code repeat. Everything between begin and repeat is executed with each iteration, but only provided that the condition continues to be met. The code between begin and while is used to evaluate the condition, thus determining whether execution will continue, or whether the loop will terminate.
For example, the following word will run while the user keeps typing an “A” (ASCII code 65) on the console, but will terminate if any other key is pressed:
For example, the following word will run while the user keeps typing an “A” (ASCII code 65) on the console, but will terminate if any other key is pressed:
: while_A
begin
key 65 =
while
." That was an A"
cr
repeat
;
Infinite Loops
Infinite loops may be implemented using begin ... again. Here is a word that, once run, will never terminate:
: semper
begin
." Once more through the loop, dear friends."
cr
again
;
Such loops are particularly useful in embedded systems, where the code must repeat forever.
Next, let's look at constants, variables and values.