Chapter 4. TurtleScript Programming Reference

This is the reference for KTurtle's TurtleScript. In the first section of this chapter have a look at some aspects of the grammar of TurtleScript programs. The second section deals exclusively with mathematical operators, boolean (true/false) operators and comparison operators. The third section is basically a giant list of all commands explaining them one-by-one. Section four explains how to assign values to variables. Finally we explain how to arrange the execution of commands with execution controlling statements in section five and how to create you own commands with learn in section six.

The Grammar of TurtleScript

As in any language, TurtleScript has different types of words and symbols. In English we distinguish verbs (like 'to walk' or 'to sing') and nouns (like 'sister' or 'house'), they are used for different purposes. TurtleScript is a programming language, it is used to instruct KTurtle what to do.

In this section some of TurtleScript's different types of words and symbols are briefly explained. We explain comments, commands and the three different kinds of literals: numbers, strings and boolean (true/false) values.

Comments

A program consists instructions that are executed when the program is run and so called comments. Comments are not executed, KTurtle simply ignores them when executing your program. Comment are there for other programmers to make them understand your program better. Everything that follows on a # symbol is considered a comment in TurtleScript. For example this little program that does nothing:

# this little program does nothing, it is only a comment!

It is a bit useless but it explain the matter well.

Comments get very useful when the program gets a little bit more complex. It can help to give some advice to other programmers. In the following program you see comments being used together with the print command.

# this program has been made by Cies Breijs.
print "this text will get printed on the canvas"
# the previous line is not a comment, but the next line is:
# print "this text will not get printed!"

The first line describes the program. The second line is executed by KTurtle and prints this text will get printed on the canvas on the canvas. The third line is a comment. And the forth line is a comment that contains a piece of TurtleScript, if the # symbol would be removed on the fourth line the print statement will we executed by KTurtle. Programmers say: the print statement on the fourth line is 'commented out'.

Commented lines are highlighted with light gray in the code editor.

Commands

Using commands you tell the turtle or KTurtle to do something. Some commands need input, some give output.

# forward is a command that needs input, in this case the number 100:
forward 100

The first line is a comment. The second line contains the forward command and the number 100. The number is not part of command, it is considered 'input' for the command.

Some commands like e.g. go need more than one input value. Multiple values have to be separated using the , character (comma).

For a detailed overview of all commands that KTurtle supports go here. Built-in commands are highlighted in dark blue

Numbers

Most likely you already know quite a bit about numbers. The way numbers are used in KTurtle is not much different from spoken language, or math.

We have the so called natural numbers: 0, 1, 2, 3, 4, 5, etc. The negative numbers: -1, -2, -3, etc. And the numbers with decimals, or dot-numbers, for example: 0.1, 3.14, 33.3333, -5.05, -1.0. The . character (dot) is used as decimal separator.

Numbers can be used in mathematical operators and comparison operators. They can also be stored in variables. Numbers are highlighted in dark red.

Strings

First an example:

print "Hello, I'm a string."

In this example print is a command where "Hello, I'm a string." is a string. Strings start and end with the " mark, by these marks KTurtle knows it is a string.

Strings can be put in variables, just like numbers. Yet, unlike numbers, strings cannot be used in mathematical operators or comparison operators. Strings are highlighted with red.

Boolean (true/false) values

There are only two boolean values: true and false. Sometimes they are also called: on and off, yes and no, one and zero. But in TurtleScript we call them, always, true and false. Have a look at this piece of TurtleScript:

$a = true

If you look in the inspector you can see that the variable $a is set to true, and has the boolean type.

Often boolean values are the result of a comparison operator, like in the following piece of TurtleScript:

$answer = 10 > 3

The variable $answer is set to true because 10 is larger than 3.

Boolean values, true and false, are highlighted with dark red.