Tymless Tutorial: Control



   
  < conditions

Conditional Loops

 
user functions >
 

Drawing

Control

Data

Reference

 

Sometimes you don't know in advance how many times to repeat a section of code. You can use a conditional loop to test an expression before each repeat. When the expression returns a zero, the loop stops. The loop counter $# tells you how many times the conditional loop has cycled.

// bigger and bigger steps
while $x lt 100
   |* $# > ~
end

The system variables $x and $y refer to the x,y coordinates of the turtle. This program keeps drawing bigger steps until the x coordinate is greater than 100.

You can also 'break out' of a do or while loop with the break command. This is usually used with an if statement inside the loop:

do 200
   if $x gt 100 break end
   |* $# > ~
end

A related command is continue. This stops processing the current repeated code section, and loops back to the start.

// bigger and bigger odd-sized steps
while $x lt 100
   if ($# mod 2) eq 0 continue
   |* $# > ~
end

 

   
  < conditions
 
user functions >