Small Basic: How to Create For Loops

How to create Loops. How to create Loops. How to create Loops.

Sometimes we need to do something a couple of times.

'do the following 3 times
   'move the tortoise 50 pixels
   'turn the tortoise 120 degrees
'repeat

This could be translated with some easy cut & pasting:

Tortise.Move(50)
Tortise.Turn(120)
 
 
Tortise.Move(50)
Tortise.Turn(120)
 
 
Tortise.Move(50)
Tortise.Turn(120)

But this quickly becomes tiresome. A better approach is to use the For Loop:

For i = 1 to 3
   Tortise.Move(50)
   Tortise.Turn(120)
EndFor

 
Notice that it seems to more closely follow the English:

'Do the following 6 times

 
...translates to:

For i = 1 to 6

 
and

'Repeat

 
becomes

EndFor

 

See Also

Other Languages