Other Looping Constructs

The other useful Smojo looping constructs are 
BEGIN
,
UNTIL
and
AGAIN
.

Example 1

This loop will never terminate
	
	: never-ending
		begin "hi" . cr again ; 
	

Example 2

Use
UNTIL
if you need to check for a condition to terminate
	
	: 5count
		0 begin 1+ dup . dup 5 > until ; 
	
	5count
	1 2 3 4 5 6 ok
	

Example 3

You can also use
WHILE-REPEAT
loops
	
	: 4chan
		0 begin 
			1+ 
			dup 5 < while "Ho!" . 
		repeat
	; 
	
	4chan
	Ho! Ho! Ho! Ho! ok
	

Quiz

Question 1

Rewrite
4CHAN
using
BEGIN
,
AGAIN
and
EXIT
.



Question 2

Write a word
SQUARES
which prints all the square numbers which are less then 1000 using the looping constructs above.



Question 3

Write a word
TRIANGLE9
which prints the following.
	1 
	2 2 
	3 3 3 
	4 4 4 4 
	5 5 5 5 5 
	6 6 6 6 6 6 
	7 7 7 7 7 7 7 
	8 8 8 8 8 8 8 8 
	9 9 9 9 9 9 9 9 9 
	



Next: Implementing Reduce