XLOGO has seven primitives which allow the construction of loops: repeat, for , while, foreach, forever, repeatwhile and repeatuntil.
repeat 4 [forward 100 left 90] # A square of side 100 repeat 6 [forward 100 left 60] # A hexagon of side 100 repeat 360 [forward 2 left 1] # A uh... 360-gon of side 2 # In short, almost a circle! |
repeat 3 [pr repcount] 1 2 3 |
for [i 1 4][pr :i*2]
2
4
6
8
# Now, i is going from 7 to 2 falling down of 1.5 each times
# Look at the negative increment
# Then, Displays its square.
for [i 7 2 -1.5 ][pr list :i power :i 2]
7 49
5.5 30.25
4 16
2.5 6.25
|
while ["true] [rt 1] # The turtle will turn around # An example which allows us to spell the alphabet in reverse make "list "abcdefghijklmnopqrstuvwxyz while [not empty? :list] [pr last :list make "list butlast :list] |
foreach "i "XLOGO [print :i]
X
L
O
G
O
foreach "i [a b c] [print :i]
a
b
c
make "sum 0 foreach "i 12345 [make "sum :sum+:i] print :sum
15
|
Eg: forever [fd 1 rt 1] |
Be careful when you use this primitive because of the infinite loop!
make "i 0 repeatwhile [pr :i make "i :i+1] [:i<4] 0 1 2 3 4 |
make "i 0 repeatuntil [pr :i make "i :i+1] [:i>4] 0 1 2 3 4 |