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


FOR Search:

In Flow Control Words in TurboForth Kernal

Word Name: FOR
Type: Immediate compiling word
Data Stack Signature: count --
Return Stack Signature: --
Availability: V1.0  V1.1  
Description:

Begins a FOR/NEXT loop.

Example:

: TEST ( -- ) 10 FOR I . NEXT ;
TEST

(displays 10 9 8 7 6 5 4 3 2 1 0)
 

Comment:

The loop begins counting at count and decreases by 1 with each iteration. The loop terminates when the loop index (accessible via I) is equal to 0. The word LEAVE may be used to conditionally exit the loop early.

Note that a FOR loop counts from count to 0, whereas a DO loop counts from count-1 to 0.

Using a FOR/NEXT loop:

: TEST 10 FOR I . NEXT ;
TEST
10 9 8 7 6 5 4 3 2 1 0
(11 values displayed)

Using a DO/LOOP:

: TEST 10 0 DO I . LOOP ;
TEST
0 1 2 3 4 5 6 7 8 9
(10 values displayed)

See Also: I  LEAVE  NEXT 

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