<< Home | About Forth | About TurboForth | Download | Language Reference | Resources | Tutorials | YouTube >>


WHILE Search:

In Flow Control Words in TurboForth Kernal

Word Name: WHILE
Type: Immediate compiling word
Data Stack Signature: flag --
Return Stack Signature: --
Availability: V1.0  V1.1  V1.2
Description:

Enters a WHILE/REPEAT block if flag evaluates to TRUE.

Example:

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

Comment:

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.

See Also: BEGIN  REPEAT 

<< Home | About Forth | About TurboForth | Download | Language Reference | Resources | Tutorials | YouTube >>