<< Home | About Forth | About TurboForth | Download | Language Reference | Resources | Tutorials | YouTube >>
In Flow Control Words in TurboForth Kernal
Enters a WHILE/REPEAT block if flag evaluates to TRUE.
WHILE
REPEAT
TRUE
The following code will add 3 repeatedly to a value entered on the stack, and exit when the value exeeds 100.
: test ( n -- ) \ add 3 repeatedly and display, until we go past 100: begin dup 100 <= while ( run the following code is the condition is true ) dup . 3 + repeat ( go back to the code jusr after BEGIN) drop ( jump to here if the condition is false) ;
7 test
WHILE is used together with BEGIN and REPEAT to execute some code if a condition is true, end then loop back again. The loop repeats until the condition preceding WHILE is not true.
BEGIN marks the start of the loop. REPEAT will jump to the code just after BEGIN and execute it again.
WHILE checks the condition, and if true, runs the code following it. If the condition is false, it jumps down to the code after REPEAT, and the loop exits.