&kturtle;'s &logo; Programming ReferenceThis is the reference for the &kturtle;'s &logo;. In this chapter we first briefly touch all the different instruction types. Then the commands are explained one by one. Then containers, math, questions and execution controllers are explained. At last you are shown how to create you own commands with learn.Different Instruction TypesAs in any language, LOGO has different types of words and symbols. Here the differences between the types are briefly explained.CommandsUsing 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
For a detailed overview of all commands that &kturtle; supports go here.NumbersMost 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.
Numbers can be used in mathematical calculations and questions. They can also be put in containers.Numbers are highlighted with blue in the code editor.StringsFirst 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 containers. Yet they cannot be used in mathematical calculations and questions.Strings are highlighted with dark red in the code editor.NamesWhen using the &logo; programming language you create new things. If you write a program you will often need containers and in some cases you need learn to create new commands. When making a container or a new command with learn you will have to specify a name.You can choose any name, as long as it does not already have a meaning. For instance you cannot name a container forward, since that name is already used for a command, and thus has a meaning.
# here forward is used as a container, but it already has a meaning
# so this will produce an error:
forward = 20
# this works:
forward 20
Names can contain only letters, numbers and underscores (_). Yet they have to start with a letter.
Please read the documentation on containers and the learn command for a better explanation and more examples.
AssignmentsAssignment are done with the = symbol. In programming languages it is better to read the single = not as 'equals' but as 'becomes'. The word 'equals' is more appropriate for the == which is a question.Assignments are generally use for two reasons, (1) to add content containers, and (2) to modify the content of a container. For example:
x = 10
# the container x now contains the number 10
W = "My age is: "
# the container W now contains the string "My age is: "
# this prints the content of the containers 'W' and 'x' on the canvas
print W + x
For more examples see the section that explains containers.Math Symbols&kturtle; supports all basic math symbols: add (+), substract (-), multiply (*), divide (/) and the brackets ( and ).For a complete explanation and more examples see the math section.QuestionsWe can ask simple questions on which the answer will be 'true' or 'false'.Using questions is extensively explained in the questions section.Question Glue-WordsQuestions can be glued together with so called 'question glue'. The glue words are and, or, and a special glue-word: not.Using question-glue is explained in the Question Glue section.CommentsComments are lines that start with a #. For example:
# this is a comment!
print "this is not a comment"
# the previous line is not a comment, but the next line is:
# print "this is not a comment"
We can add comments to the code for ourselves or for someone else to read. Comments are used for: (1) adding a small description to the program, (2) explaining how a piece of code works if it is a bit cryptic, and (3) to 'comment-out' lines of code that should be (temporarily) ignored (see the last line of the example).Commented lines are highlighted with dark yellow in the code editor.CommandsUsing commands you tell the turtle or &kturtle; to do something. Some commands need input, some give output. In this section we explain all the commands that can be used in &kturtle;. Please note that all build in commands we discuss here are highlighted with dark green in the code editor, this can help you to distinguish them.Moving the turtleThere are several commands to move the turtle over the screen.forward (fw)forwardforward Xforward moves the turtle forward by the amount of X pixels. When the pen is down the turtle will leave a trail. forward can be abbreviated to fwbackward (bw)backwardbackward Xbackward moves the turtle backward by the amount of X pixels. When the pen is down the turtle will leave a trail. backward can be abbreviated to bw.turnleft (tl)turnleftturnleft Xturnleft commands the turtle to turn an amount of X degrees to the left. turnleft can be abbreviated to tl.turnright (tr)turnrightturnright Xturnrightthe turtle to turn an amount of X degrees to the right. turnright can be abbreviated to tr.direction (dir)directiondirection Xdirection set the turtle's direction to an amount of X degrees counting from zero, and thus is not relative to the turtle's previous direction. direction can be abbreviated to dir.centercentercentercenter moves the turtle to the center on the canvas.gogogo X,Ygo commands the turtle to go to a certain place on the canvas. This place is X pixels from the left of the canvas, and Y pixels form the top of the canvas. Note that using the go command the turtle will not draw a line.goxgoxgox Xgox using this command the turtle will move to X pixels from the left of the canvas whilst staying at the same height.goygoygoy Ygox using this command the turtle will move to Y pixels from the top of the canvas whilst staying at the same distance from the left border of the canvas.The turtle has a penThe turtle has a pen that draws a line when the turtle moves. There are a few commands to control the pen. In this section we explain these commands.penup (pu)penuppenuppenup lifts the pen from the canvas. When the pen is up no line will be drawn when the turtle moves. See also pendown. penup can be abbreviated to pu.pendown (pd)pendownpendownpendown presses the pen down on the canvas. When the pen is press down on the canvas a line will be drawn when the turtle moves. See also penup. pendown can be abbreviated to pd.penwidth (pw)penwidthpenwidth Xpenwidth sets the width of the pen (the line width) to an amount of X pixels. penwidth can be abbreviated to pw.pencolor (pc)pencolorpencolor R,G,Bpencolor sets the color of the pen. pencolor takes an RGB combination as input. pencolor can be abbreviated to pc.Commands to control the canvasThere are several commands to control the canvas.canvassize (cs)canvassizecanvassize X,Y
With the canvassize command you can set the size of the canvas. It takes X and Y as input, where X is the new canvas width in pixels, and Y is the new height of the canvas in pixels. canvassize can be abbreviated to cs.canvascolor (cc)canvascolorcanvascolor R,G,Bcanvascolor set the color of the canvas. canvascolor takes an RGB combination as input. canvascolor can be abbreviated to cc.wraponwraponwrapon
With the wrapon command you can set wrappingon for the canvas. Please see the glossary if you want to know what wrapping is.wrapoffwrapoffwrapoff
With the wrapoff command you can set wrappingoff for the canvas: this means the turtle can move off the canvas and can get lost. Please see the glossary if you want to know what wrapping is.Commands to clean upThere are two commands to clean up the canvas after you have made a mess.clear (cr)clearclear
With clear you can clean all drawings from the canvas. All other things remain: the position and angle of the turtle, the canvascolor, the visibility of the turtle, and the canvas size. clear can be abbreviated to cr.resetresetresetreset cleans much more thoroughly than the clear command. After a reset command everything is like is was when you had just started &kturtle;. The turtle is positioned at the middle of the screen, the canvas color is white, and the turtle draws a black line on the canvas.The turtle is a spriteFirst a brief explanation of what sprites are: sprites are small pictures that can be moved around the screen, like we often see in computer games. Our turtle is also a sprite. For more info see the glossary on sprites. Next you will find a full overview on all commands to work with sprites.[The current version of &kturtle; does not yet support the use of sprites other than the turtle. With future versions you will be able to change the turtle into something of your own design]showshow (ss)showshow makes the turtle visible again after it has been hidden. show can be abbreviated to ss.hide (sh)hidehidehide hides the turtle. This can be used if the turtle does not fit in your drawing. hide can be abbreviated to sh.Can the turtles write?The answer is: yes. The turtle can write: it writes just about everything you command it to.printprintprint X
The print command is used to command the turtle to write something on the canvas. print takes numbers and strings as input. You can print various numbers and strings using the + symbol. See here a small example:
year = 2003
author = "Cies"
print author + " started the KTurtle project in " + year + " and still enjoys working on it!"
fontsizefontsizefontsize Xfontsize sets the size of the font that is used by print. fontsize takes one input which should be a number. The size is set in pixels.A command that rolls dice for youThere is one command that rolls dice for you, it is called random, and it is very useful for some unexpected results.randomrandom X,Yrandom is a command that takes input and gives output. As input are required two numbers, the first (X) sets the minimum output, the second (Y) sets the maximum. The output is a randomly chosen number that is equal or greater then the minimum and equal or smaller than the maximum. Here a small example:
repeat 500 [
x = random 1,20
forward x
turnleft 10 - x
]
Using the random command you can add a bit of chaos to your program.Input and feedback though dialogsA dialog is a small pop-up window that provides some feedback or asks for some input. &kturtle; has two commands for dialogs, namely: message and inputwindowmessagemessagemessage X
The message command takes a string as input. It shows a pop-up dialog containing the text from the string.
year = 2003
author = "Cies"
print author + " started the KTurtle project in " + year + " and still enjoys working on it!"
inputwindowinputwindowinputwindow Xinputwindow takes a string as input. It shows a pop-up dialog containing the text from the string, just like the message. But in addition to it also puts an input field on the dialog. Through this input filed the user can enter a number or a string which can be stored in a container. For example
in = inputwindow "What is you age?"
out = 2003 - in
print "In 2003 you where " + out + " years old at some point."
When a user cancels the input dialog, or does not enter anything at all the container is emptied.ContainersContainers are letters or words that can be used by the programmer to store a number or a text. Containers that contain a number are called variables, containers that can contain text are called string.Containers that are not used contain nothing. An example:
print N
This will print nothing. If we try to do math with empty containers we will get errors.
Variables: number containersLet us start with an example:
x = 3
print x
In the first line the letter x made into a variable (number container). As you see the value of the variable x is set to 3. On the second line the value is printed. Note that if we wanted to print an x that we should have written
print "x"
That was easy, now a bit harder example:
A = 2004
B = 25
C = A + B
# the next command prints "2029"
print C
backward 30
# the next command prints "2004 plus 25"
print A + " plus " + B
backward 30
# the next command prints "1979"
print A - B
In the first two lines the variables A and B are set to 2004 and 25. On the third line the variable C is set to A + B, which is 2029. The rest of the example consists of 3 print commands with backward 30 in between. The backward 30 is there to make sure every output is on a new line. In this example you also see that variables can be used in mathematical calculations.Containers that contain text (strings)In programming code the regular text is usually started and ended with quotes. As we have already seen:
print "Hello programmer!"
The regular is delimited with quotes. These pieces of regular text we call strings.Strings can also be stored in containers just like numbers
Strings are a lot like variables. The biggest difference is that they contain text in stead of numbers. For this reason strings cannot be used in mathematical calculations and questions. An example of the use of strings:
x = "Hello "
name = inputwindow "Please enter your name..."
print x + name + ", how are you?"
On the first line the string x is set to Hello . On the second line the string name is set to the output of the inputwindow command. On the third line the program prints a composition of three strings on the canvas.This program ask you to enter your name. When you, for instance, enter the name Paul, the program prints Hello Paul, how are you?. Please note that the plus (+) is the only math symbol that you can use with strings.Can the Turtle do math?Yes, &kturtle; will do your math. You can add (+), substract (-), multiply (*), and divide (/). Here is an example in which we use all of them:
a = 20 - 5
b = 15 * 2
c = 30 / 30
d = 1 + 1
print "a: "+a+", b: "+b+", c: "+c+", d: "+d
Do you know what value a, b, c and d have? Please note the use of the assignment symbol =.If you just want a simple calculation to be done you can do something like this:
print 2004-12
Now an example with parentheses:
print ( ( 20 - 5 ) * 2 / 30 ) + 1
The expressions inside parentheses will be calculated first. In this example, 20-5 will be calculated, then multiplied by 2, divided by 30, and then 1 is added (giving 2).Asking questions, getting answers...if and while are execution controllers that we will discuss in the next section. In this section we use the if command to explain questions.QuestionsA simple example of a question:
x = 6
if x > 5 [
print "hello"
]
In this example the question is the x > 5 part. If the answer to this question is 'true' the code between the brackets will be executed. Questions are an important part of programming and often used together with execution controllers, like if. All numbers and variables (number containers) can be compared to each other with questions.
Here are all possible questions:
Types of questionsa == bequalsanswer is true if a equals ba != bnot-equalanswer is true if a does not equal ba > bgreater thananswer is true if a is greater than ba < bsmaller thananswer is true if a is smaller than ba >= bgreater than or equalsanswer is true if a is greater than or equals ba <= bsmaller than or equalsanswer is true if a is smaller than or equals b
Questions are highlighted with light blue in the code editor.Question Glue
Question glue-words enable us to glue questions into one big question.
a = 1
b = 5
if (a < 5) and (b == 5) [
print "hello"
]
In this example the glue-word and is used to glue 2 questions (a < 5, b == 5) together. If one side of the and would answer false the whole question would answer false, because with the glue-word and both sides need to be true in order to answer true. Please do not forget to use the brackets around the questions!
Here is a schematic overview; a more detailed explanation follows below:
Question glue-wordsandBoth sides need to be 'true' in order to answer 'true'orIf one of the sides is 'true' the answer is 'true'notSpecial case: only works on one question! Changes 'true' into 'false' and 'false' into 'true'.
Question glue-words are highlighted with purple in the code editor.andWhen two questions are glued together with and, both sides of the and have to be 'true' in order to result in 'true'. An example:
a = 1
b = 5
if ((a < 10) and (b == 5)) and (a < b) [
print "hello"
]
In this example you see a glued question glued onto an other question.orIf one of the two questions that are glued together with or is 'true' the result will be 'true'. An example:
a = 1
b = 5
if ((a < 10) or (b == 10)) or (a == 0) [
print "hello"
]
In this example you see a glued question glued onto an other question.notnot is a special question glue-word because it only works for one question at the time. not changes 'true' into 'false' and 'false' into 'true'. An example:
a = 1
b = 5
if not ((a < 10) and (b == 5)) [
print "hello"
]
else
[
print "not hello ;-)"
]
In this example the glued question is 'true' yet the not changes it to 'false'. So in the end "not hello ;-)" is printed on the canvas.Controlling executionThe 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 waitIf 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.waitwait Xwait 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"ifif 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 executedThe "while" loopwhilewhile 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"elseif 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 loopforfor start point to end point [ ... ]
The for loop is a counting loop, &ie; 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.Create your own commands with learnlearn is a very special command, because it is used to create your own commands. The command you create can take input and return output. Let us take a look at how a new command is created:
learn circle x [
repeat 36 [
forward x
turnleft 10
]
]
The new command is called circle. circle takes one input, a number, to set the size of the circle. circle returns no output. The circle command can now be used like a normal command in the rest of the code. See this example:
learn circle X [
repeat 36 [
forward X
turnleft 10
]
]
go 30,30
circle 20
go 40,40
circle 50
In the next example, a command with a return value is created.
reset
learn multiplyBySelf n [
r = n * 1
r = n * n
return r
]
i = inputwindow "Please enter a number and press OK"
print i + " multiplied by itself is: " + multiplyBySelf i
In this example a new command called multiplyBySelf is created. The input of this command is multiplied by itself and then returned, using the return command. The return command is the way to output a value from a function you have created.