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 brackets are mostly used together with execution controllers and they are highlighted with black.

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 boolean { ... }

The code that is placed between the brackets will only be executed if the boolean value evaluates true.

$x = 6
if $x > 5 {
  print "$x is greater than five!"
}

On the first line $x is set to 6. On the second line a comparing operator is used to evaluate $x > 5. Since this evaluates true, 6 is larger than 5, the execution controller if will allow the code between the brackets to be executed.

If not, in other words: else

else
if boolean { ... } else { ... }

else can be used in addition to the execution controller if. The code between the brackets after else is only executed if the boolean evaluates false.

reset
$x = 4
if $x > 5 {
  print "$x is greater than five!"
} else {
  print "$x is smaller than six!"
}

The comparing operator evaluates the expression $x > 5. Since 4 is not greater than 5 the expression evaluates false. This means the code between the brackets after else gets executed.

The while loop

while
while boolean { ... }

The execution controller while is a lot like if. The difference is that while keeps repeating (looping) the code between the brackets until the boolean evaluates 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 $x < 5 is evaluated. Since the answer to this question is true the execution controller while starts executing the code between the brackets until the $x < 5 evaluates 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.

The repeat loop

repeat
repeat number { ... }

The execution controller repeat is a lot like while. The difference is that repeat keeps repeating (looping) the code between the brackets for as many times as the given number.

The for loop, a counting loop

for
for variable = number to number { ... }

The for loop is a counting loop, i.e. it keeps count for you. The first number sets the variable to the value in the first loop. Every loop the number is increased until the second number is reached.

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.

The default step size of a loop is 1, you can use an other value with

for variable = number to number step number { ... }

Leave a loop

break
break

Terminates the current loop immediately and transfers control to the statement immediately following that loop.

Stop executing your program

exit
exit

Finishes the execution of your program.

Checking assertions at runtime

assert
assert boolean

Can be used to reason about program or input correctness.

$in = ask "What is your year of birth?"
# the year must be positive
assert $in > 0