OpenBoot 3.x Command Reference Manual

The begin Loop

A begin loop executes the same commands repeatedly until a certain condition is satisfied. Such a loop is also called a conditional loop.

Table 4-30 lists commands to control the execution of conditional loops.

Table 4-30 begin (Conditional) Loop Commands


Stack Diagram 



( -- ) 

End a beginagain infinite loop.


( -- ) 

Begin a beginwhilerepeat, beginuntil, or beginagain loop.


( -- ) 

End a beginwhilerepeat loop.


( flag -- ) 

Continue executing a beginuntil loop until flag is true.


( flag -- ) 

Continue executing a beginwhilerepeat loop while flag is true.

There are two general forms:

begin 		any commandsflag until 


begin			any commands				flag		while
			more commands						repeat

In both cases, the commands in the loop are executed repeatedly until the proper flag value causes the loop to be terminated. Then execution continues normally with the command following the closing command word (until or repeat).

In the beginuntil case, until removes a flag from the top of the stack and inspects it. If the flag is false, execution continues just after the begin, and the loop repeats. If the flag is true, the loop is exited.

In the beginwhilerepeat case, while removes a flag from the top of the stack and inspects it. If the flag is true, the loop continues by executing the commands just after the while. The repeat command automatically sends control back to begin to continue the loop. If the flag is false when while is encountered, the loop is exited immediately; control goes to the first command after the closing repeat.

An easy mnemonic for either of these loops is: If true, fall through.

A simple example follows.

ok begin 4000 c@ . key? until   (
repeat until any key is pressed)
43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43

The loop starts by fetching a byte from location 4000 and displaying the value. Then, the key? command is called, which leaves a true on the stack if the user has pressed any key, and false otherwise. This flag is consumed by until and, if the value is false, then the loop continues. Once a key is pressed, the next call to key? returns true, and the loop terminates.

Unlike many versions of Forth, the User Interface allows the interactive use of loops and conditionals -- that is, without first creating a definition.