DO Loops

The power of computers is that they don't get tired of doing the same thing again and again.

The pair DO ... LOOP are words to automate such repetition. For example, this program:

		: counter 
			0 DO  
				i . 
			LOOP ;
			 
		5 counter
	
will produce the result:
0 1 2 3 4 ok

DO
expects 2 items on the stack: the end and start of the loop index. In the example above, what is the start index? What is the end index? The word
I
pushes the current value of the loop index onto the stack.

Important Points

Quiz

Question 1

Amend the program so that it prints numbers from 0 to 15.



Question 2

Amend 
COUNTER
so that it prints numbers starting from 1 instead of 0.



Question 3

How to print the end index also, instead of stopping short of it?



Question 4

Amend 
COUNTER
so that it prints three times the value of the loop index.



Test all your answers before continuing.

Next: More on Loops