Controlling execution
Prev
Next

Controlling execution

The execution controllers enable you — as their name implies — to control execution.

Execution controlling commands are highlighted with dark green in a bold font type. The square brackets are mostly used together with execution controllers and they are highlighted with light green.

Have the turtle wait

If you have done some programming in KTurtle you have might noticed that the turtle can be very quick at drawing. This command makes the turtle wait for a given amount of time.

wait

wait X
wait makes the turtle wait for X seconds.
repeat 36 [
  forward 5
  turnright 10
  wait 0.5
]
This code draws a circle, but the turtle will wait half a second after each step. This gives the impression of a slow-moving turtle.

Execute "if"

if

if question [ ... ]
The code that is placed between the brackets will only be executed if the answer to the question is “true”. Please read for more information on questions in the question section.
x = 6
if x > 5 [
  print "x is greater than five!"
]
On the first line x is set to 6. On the second line the question x > 5 is asked. Since the answer to this question is “true” the execution controller if will allow the code between the brackets to be executed

The "while" loop

while

while question [ ... ]
The execution controller while is a lot like if. The difference is that while keeps repeating (looping) the code between the brackets until the answer to the question is “false”.
x = 1
while x < 5 [
  forward 10
  wait 1
  x = x + 1
]
On the first line x is set to 1. On the second line the question x < 5 is asked. Since the answer to this question is “true” the execution controller while starts executing the code between the brackets until the answer to the question is “false”. In this case the code between the brackets will be executed 4 times, because every time the fifth line is executed x increases by 1.

If not, in other words: "else"

else

if question [ ... ] else [ ... ]
else can be used in addition to the execution controller if. The code between the brackets after else is only executed if the answer to the question that is asked is “false”.
reset
x = 4
if x > 5 [
  print "x is greater than five!"
]
else
[
  print "x is smaller than six!"
]
The question asks if x is greater than 5. Since x is set to 4 on the first line the answer to the question is “false”. This means the code between the brackets after else gets executed.

The "for" loop, a counting loop

for

for start point to end point [ ... ]
The for loop is a “counting loop”, i.e. it keeps count for you.
for x = 1 to 10 [
  print x * 7
  forward 15
]
Every time the code between the brackets is executed the x is increased by 1, until x reaches the value of 10. The code between the brackets prints the x multiplied by 7. After this program finishes its execution you will see the times table of 7 on the canvas.

Prev
Next
Home


Would you like to make a comment or contribute an update to this page?
Send feedback to the KDE Docs Team