moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / doc / kturtle / programming-reference.docbook
blobcd88830528b325c370a761cd6f6ea9225e0ddf05
1 <!--Dear translator: please NEVER translate the id or anything inside the tags as they are needed in english by the application
2      Thanks a lot in advance.-->
3 <chapter id="reference">
4 <title>&kturtle;'s &logo; Programming Reference</title>
5 <para>This is the reference for the &kturtle;'s &logo;. In this chapter we first briefly touch all the <link linkend="different-instructions">different instruction types</link>. Then the <link linkend="commands">commands</link> are explained one by one. Then <link linkend="containers">containers</link>, <link linkend="math">math</link>, <link linkend="questions">questions</link> and <link linkend="controlling-execution">execution controllers</link> are explained. At last you are shown how to create you own commands with <link linkend="learn">learn</link>.</para>
7 <sect1 id="different-instructions">
8 <title>Different Instruction Types</title>
9 <para>As in any language, LOGO has different types of words and symbols. Here the differences between the types are briefly explained.</para>
11 <sect2 id="command">
12 <title>Commands</title>
13 <para>Using commands you tell the turtle or &kturtle; to do something. Some commands need input, some give output.
14 <screen>
15 # forward is a command that needs input, in this case the number 100:
16 forward 100
17 </screen>
18 </para>
19 <para>For a detailed overview of all commands that &kturtle; supports go <link linkend="commands">here</link>.</para>
20 </sect2>
22 <sect2 id="number">
23 <title>Numbers</title>
24 <para>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. </para>
25 <para>We have the so called natural numbers: <userinput>0</userinput>, <userinput>1</userinput>, <userinput>2</userinput>, <userinput>3</userinput>, <userinput>4</userinput>, <userinput>5</userinput>, etc. The negative numbers: <userinput>-1</userinput>, <userinput>-2</userinput>, <userinput>-3</userinput>, etc. And the numbers with decimals, or dot-numbers, for example: <userinput>0.1</userinput>, <userinput>3.14</userinput>, <userinput>33.3333</userinput>, <userinput>-5.05</userinput>, <userinput>-1.0</userinput>.
26 </para>
27 <para>Numbers can be used in <link linkend="math">mathematical calculations</link> and <link linkend="questions">questions</link>. They can also be put in <link linkend="containers">containers</link>.</para>
28 <para>Numbers are <glossterm>highlighted</glossterm> with blue in the <link linkend="the-code-editor">code editor</link>.</para>
29 </sect2>
31 <sect2 id="string">
32 <title>Strings</title>
33 <para>First an example:
34 <screen>
35 print "Hello, I'm a string."
36 </screen>
37 In this example <userinput>print</userinput> is a command where <userinput>"Hello, I'm a string."</userinput> is a string. Strings start and end with the <userinput>"</userinput> mark, by these marks &kturtle; knows it is a string.</para>
38 <para>Strings can be put in <link linkend="containers">containers</link>. Yet they cannot be used in <link linkend="math">mathematical calculations</link> and <link linkend="questions">questions</link>.</para>
39 <para>Strings are <glossterm>highlighted</glossterm> with dark red in the <link linkend="the-code-editor">code editor</link>.</para>
40 </sect2>
43 <sect2 id="name">
44 <title>Names</title>
45 <para>When using the &logo; programming language you create new things. If you write a program you will often need <link linkend="containers">containers</link> and in some cases you need <link linkend="learn">learn</link> to create new commands. When making a <link linkend="containers">container</link> or a new command with <link linkend="learn">learn</link> you will have to specify a name.</para>
46 <para>You can choose any name, as long as it does not already have a meaning. For instance you cannot name a container <link linkend="forward">forward</link>, since that name is already used for a command, and thus has a meaning.
47 <screen>
48 # here forward is used as a container, but it already has a meaning
49 # so this will produce an error:
50 forward = 20
52 # this works:
53 forward 20
54 </screen>
55 Names can contain only letters, numbers and underscores (_). Yet they have to start with a letter. 
56 </para>
57 <para>
58 Please read the documentation on <link linkend="containers">containers</link> and the <link linkend="learn">learn</link> command for a better explanation and more examples.
59 </para>
60 </sect2>
62 <sect2 id="assignment">
63 <title>Assignments</title>
64 <para>Assignment are done with the <userinput>=</userinput> symbol. In programming languages it is better to read the single <userinput>=</userinput> not as 'equals' but as 'becomes'. The word 'equals' is more appropriate for the <userinput>==</userinput> which is a <link linkend="questions">question</link>.</para>
65 <para>Assignments are generally use for two reasons, (1) to add content <link linkend="containers">containers</link>, and (2) to modify the content of a container. For example:
66 <screen>
67 x = 10
68 # the container x now contains the number 10
69 W = "My age is: "
70 # the container W now contains the string "My age is: "
71 # this prints the content of the containers 'W' and 'x' on the canvas
72 print W + x
73 </screen>
74 </para>
75 <para>For more examples see the section that explains <link linkend="containers">containers</link>.</para>
76 </sect2>
78 <sect2 id="math-symbols">
79 <title>Math Symbols</title>
80 <para>&kturtle; supports all basic math symbols: add (<userinput>+</userinput>), substract (<userinput>-</userinput>), multiply (<userinput>*</userinput>), divide (<userinput>/</userinput>) and the brackets <userinput>(</userinput> and <userinput>)</userinput>.</para>
81 <para>For a complete explanation and more examples see the <link linkend="math">math</link> section.</para>
82 </sect2>
84 <sect2 id="question">
85 <title>Questions</title>
86 <para>We can ask simple questions on which the answer will be 'true' or 'false'.</para>
87 <para>Using questions is extensively explained in the <link linkend="questions">questions</link> section.</para>
88 </sect2>
90 <sect2 id="questions-glue">
91 <title>Question Glue-Words</title>
92 <para>Questions can be glued together with so called 'question glue'. The glue words are <userinput>and</userinput>, <userinput>or</userinput>, and a special glue-word: <userinput>not</userinput>.</para>
93 <para>Using question-glue is explained in the <link linkend="question-glue">Question Glue</link> section.</para>
94 </sect2>
97 <sect2 id="comment">
98 <title>Comments</title>
99 <para>Comments are lines that start with a <userinput>#</userinput>. For example:
100 <screen>
101 # this is a comment!
102 print "this is not a comment"
103 # the previous line is not a comment, but the next line is:
104 # print "this is not a comment"
105 </screen>
106 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).</para>
107 <para>Commented lines are <glossterm>highlighted</glossterm> with dark yellow in the <link linkend="the-code-editor">code editor</link>.</para>
108 </sect2>
110 </sect1>
113 <sect1 id="commands">
114 <title>Commands</title>
115 <para>Using 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 <glossterm>highlighted</glossterm> with dark green in the <link linkend="the-code-editor">code editor</link>, this can help you to distinguish them.</para>
117 <sect2 id="moving-the-turtle">
118 <title>Moving the turtle</title>
119 <para>There are several commands to move the turtle over the screen.</para>
120   
121 <sect3 id="forward">
122   <title>forward (fw)</title>
123   <variablelist>
124     <varlistentry> 
125       <term>forward</term>
126       <listitem><para><screen>forward X</screen>
127       <userinput>forward</userinput> moves the turtle forward by the amount of X pixels. When the pen is down the turtle will leave a trail. <userinput>forward</userinput> can be abbreviated to <userinput>fw</userinput></para></listitem>
128     </varlistentry>
129   </variablelist>
130 </sect3>
131 <sect3 id="backward">
132   <title>backward (bw)</title>
133   <variablelist>
134     <varlistentry>  
135       <term>backward</term>
136       <listitem><para><screen>backward X</screen>
137       <userinput>backward</userinput> moves the turtle backward by the amount of X pixels. When the pen is down the turtle will leave a trail. <userinput>backward</userinput> can be abbreviated to <userinput>bw</userinput>.</para></listitem>
138     </varlistentry>
139   </variablelist>
140 </sect3>
141 <sect3 id="turnleft">
142   <title>turnleft (tl)</title>
143   <variablelist>
144     <varlistentry> 
145       <term>turnleft</term>
146       <listitem><para><screen>turnleft X</screen>
147       <userinput>turnleft</userinput> commands the turtle to turn an amount of X degrees to the left. <userinput>turnleft</userinput> can be abbreviated to <userinput>tl</userinput>.</para></listitem>
148     </varlistentry>
149   </variablelist>
150 </sect3>
151 <sect3 id="turnright">
152   <title>turnright (tr)</title>
153   <variablelist>
154     <varlistentry> 
155       <term>turnright</term>
156       <listitem><para><screen>turnright X</screen>
157       <userinput>turnright</userinput>the turtle to turn an amount of X degrees to the right. <userinput>turnright</userinput> can be abbreviated to <userinput>tr</userinput>.</para></listitem>
158     </varlistentry>
159   </variablelist>
160 </sect3>
161 <sect3 id="direction">
162   <title>direction (dir)</title>
163   <variablelist>
164     <varlistentry> 
165       <term>direction</term>
166       <listitem><para><screen>direction X</screen>
167       <userinput>direction</userinput> 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. <userinput>direction</userinput> can be abbreviated to <userinput>dir</userinput>.</para></listitem>
168     </varlistentry>
169   </variablelist>
170 </sect3>
171 <sect3 id="center">
172   <title>center</title>
173   <variablelist>
174     <varlistentry> 
175       <term>center</term>
176       <listitem><para><screen>center</screen>
177       <userinput>center</userinput> moves the turtle to the center on the canvas.</para></listitem>
178     </varlistentry>
179   </variablelist>
180 </sect3>
181 <sect3 id="go">
182   <title>go</title>
183   <variablelist>
184     <varlistentry> 
185       <term>go</term>
186       <listitem><para><screen>go X,Y</screen>
187       <userinput>go</userinput> commands the turtle to go to a certain place on the canvas. This place is X <glossterm linkend="pixels">pixels</glossterm> from the left of the canvas, and Y <glossterm linkend="pixels">pixels</glossterm> form the top of the canvas. Note that using the <userinput>go</userinput> command the turtle will not draw a line.</para></listitem>
188     </varlistentry>
189   </variablelist>
190 </sect3>
191 <sect3 id="gox">
192   <title>gox</title>
193   <variablelist>
194     <varlistentry> 
195       <term>gox</term>
196       <listitem><para><screen>gox X</screen>
197       <userinput>gox</userinput> using this command the turtle will move to X <glossterm linkend="pixels">pixels</glossterm> from the left of the canvas whilst staying at the same height.</para></listitem>
198     </varlistentry>
199   </variablelist>
200 </sect3>
201 <sect3 id="goy">
202   <title>goy</title>
203   <variablelist>
204     <varlistentry> 
205       <term>goy</term>
206       <listitem><para><screen>goy Y</screen>
207       <userinput>gox</userinput> using this command the turtle will move to Y <glossterm linkend="pixels">pixels</glossterm> from the top of the canvas whilst staying at the same distance from the left border of the canvas.</para></listitem>
208     </varlistentry>
209   </variablelist>
210 </sect3>
211 </sect2>
213 <sect2 id="pen">
214 <title>The turtle has a pen</title>
215 <para>The 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.</para>
216 <sect3 id="penup">
217   <title>penup (pu)</title>
218   <variablelist>
219     <varlistentry> 
220       <term>penup</term>
221       <listitem><para><screen>penup</screen>
222       <userinput>penup</userinput> lifts the pen from the canvas. When the pen is <quote>up</quote> no line will be drawn when the turtle moves. See also <userinput>pendown</userinput>. <userinput>penup</userinput> can be abbreviated to <userinput>pu</userinput>.</para></listitem>
223     </varlistentry>
224   </variablelist>
225 </sect3>
226 <sect3 id="pendown">
227   <title>pendown (pd)</title>
228   <variablelist>
229     <varlistentry> 
230       <term>pendown</term>
231       <listitem><para><screen>pendown</screen>
232       <userinput>pendown</userinput> presses the pen down on the canvas. When the pen is press <quote>down</quote> on the canvas a line will be drawn when the turtle moves. See also <userinput>penup</userinput>. <userinput>pendown</userinput> can be abbreviated to <userinput>pd</userinput>.</para></listitem>
233     </varlistentry>
234   </variablelist>
235 </sect3>
236 <sect3 id="setpenwidth">
237   <title>penwidth (pw)</title>
238   <variablelist>
239     <varlistentry> 
240       <term>penwidth</term>
241       <listitem><para><screen>penwidth X</screen>
242       <userinput>penwidth</userinput> sets the width of the pen (the line width) to an amount of X <glossterm linkend="pixels">pixels</glossterm>. <userinput>penwidth</userinput> can be abbreviated to <userinput>pw</userinput>.</para></listitem>
243     </varlistentry>
244   </variablelist>
245 </sect3>
246 <sect3 id="setfgcolor">
247   <title>pencolor (pc)</title>
248   <variablelist>
249     <varlistentry> 
250       <term>pencolor</term>
251       <listitem><para><screen>pencolor R,G,B</screen>
252       <userinput>pencolor</userinput> sets the color of the pen. <userinput>pencolor</userinput> takes an <glossterm linkend="rgb">RGB combination</glossterm> as input. <userinput>pencolor</userinput> can be abbreviated to <userinput>pc</userinput>.</para></listitem>
253     </varlistentry>
254   </variablelist>
255 </sect3>
256 </sect2>
258 <sect2 id="canvas">
259 <title>Commands to control the canvas</title>
260 <para>There are several commands to control the canvas.</para>
261 <sect3 id="resizecanvas">
262   <title>canvassize (cs)</title>
263   <variablelist>
264     <varlistentry>
265       <term>canvassize</term>
266       <listitem><para><screen>canvassize X,Y</screen>
267       With the <userinput>canvassize</userinput> command you can set the size of the canvas. It takes X and Y as input, where X is the new canvas width in <glossterm linkend="pixels">pixels</glossterm>, and Y is the new height of the canvas in <glossterm linkend="pixels">pixels</glossterm>. <userinput>canvassize</userinput> can be abbreviated to <userinput>cs</userinput>.</para></listitem>
268     </varlistentry>
269   </variablelist>
270 </sect3>
271 <sect3 id="setbgcolor">
272   <title>canvascolor (cc)</title>
273   <variablelist>
274     <varlistentry> 
275       <term>canvascolor</term>
276       <listitem><para><screen>canvascolor R,G,B</screen>
277       <userinput>canvascolor</userinput> set the color of the canvas. <userinput>canvascolor</userinput> takes an <glossterm linkend="rgb">RGB combination</glossterm> as input. <userinput>canvascolor</userinput> can be abbreviated to <userinput>cc</userinput>.</para></listitem>
278     </varlistentry>
279   </variablelist>
280 </sect3>
281 <sect3 id="wrapon">
282   <title>wrapon</title>
283   <variablelist>
284     <varlistentry> 
285       <term>wrapon</term>
286       <listitem><para><screen>wrapon</screen>
287       With the <userinput>wrapon</userinput> command you can set <glossterm linkend="wrapping">wrapping</glossterm> <quote>on</quote> for the canvas. Please see the glossary if you want to know what <glossterm linkend="wrapping">wrapping</glossterm> is.</para></listitem>
288     </varlistentry>
289   </variablelist>
290 </sect3>
291 <sect3 id="wrapoff">
292   <title>wrapoff</title>
293   <variablelist>
294     <varlistentry> 
295       <term>wrapoff</term>
296       <listitem><para><screen>wrapoff</screen>
297       With the <userinput>wrapoff</userinput> command you can set <glossterm linkend="wrapping">wrapping</glossterm> <quote>off</quote> for the canvas: this means the turtle can move off the canvas and can get <quote>lost</quote>. Please see the glossary if you want to know what <glossterm linkend="wrapping">wrapping</glossterm> is.</para></listitem>
298     </varlistentry>
299   </variablelist>
300 </sect3>
301 </sect2>
303 <sect2 id="clean">
304 <title>Commands to clean up</title>
305 <para>There are two commands to clean up the canvas after you have made a mess.</para>
306 <sect3 id="clear">
307   <title>clear (cr)</title>
308   <variablelist>
309     <varlistentry> 
310       <term>clear</term>
311       <listitem><para><screen>clear</screen>
312       With <userinput>clear</userinput> 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. <userinput>clear</userinput> can be abbreviated to <userinput>cr</userinput>.</para></listitem>
313     </varlistentry>
314   </variablelist>
315 </sect3>
316 <sect3 id="reset">
317   <title>reset</title>
318   <variablelist>
319     <varlistentry> 
320       <term>reset</term>
321       <listitem><para><screen>reset</screen>
322       <userinput>reset</userinput> cleans much more thoroughly than the <userinput>clear</userinput> command. After a <userinput>reset</userinput> 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.</para></listitem>
323     </varlistentry>
324   </variablelist>
325 </sect3>
326 </sect2>
328 <sect2 id="sprites">
329 <title>The turtle is a sprite</title>
330 <para>First 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 <glossterm linkend="sprites">sprites</glossterm>. </para>
331 <para>Next you will find a full overview on all commands to work with sprites.</para>
332 <para>[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]</para>
333 <sect3 id="spriteshow">
334   <title>show</title>
335   <variablelist>
336     <varlistentry> 
337       <term>show (ss)</term>
338       <listitem><para><screen>show</screen>
339       <userinput>show</userinput> makes the turtle visible again after it has been hidden. <userinput>show</userinput> can be abbreviated to <userinput>ss</userinput>.</para></listitem>
340     </varlistentry>
341   </variablelist>
342 </sect3>
343 <sect3 id="spritehide">
344   <title>hide (sh)</title>
345   <variablelist>
346     <varlistentry> 
347       <term>hide</term>
348       <listitem><para><screen>hide</screen>
349       <userinput>hide</userinput> hides the turtle. This can be used if the turtle does not fit in your drawing. <userinput>hide</userinput> can be abbreviated to <userinput>sh</userinput>.</para></listitem>
350     </varlistentry>
351   </variablelist>
352 </sect3>
353 </sect2>
355 <sect2 id="writing">
356 <title>Can the turtles write?</title>
357 <para>The answer is: <quote>yes</quote>. The turtle can write: it writes just about everything you command it to.</para>
358 <sect3 id="print">
359   <title>print</title>
360   <variablelist>
361     <varlistentry> 
362       <term>print</term>
363       <listitem><para><screen>print X</screen>
364       The <userinput>print</userinput> command is used to command the turtle to write something on the canvas. <userinput>print</userinput> takes numbers and strings as input. You can <userinput>print</userinput> various numbers and strings using the <quote>+</quote> symbol. See here a small example:
365 <screen>
366 year = 2003
367 author = "Cies"
368 print author + " started the KTurtle project in " + year + " and still enjoys working on it!"
369 </screen>
370       </para></listitem>
371     </varlistentry>
372   </variablelist>
373 </sect3>
374 <sect3 id="fontsize">
375   <title>fontsize</title>
376   <variablelist>
377     <varlistentry> 
378       <term>fontsize</term>
379       <listitem><para><screen>fontsize X</screen>
380       <userinput>fontsize</userinput> sets the size of the font that is used by <userinput>print</userinput>. <userinput>fontsize</userinput> takes one input which should be a number. The size is set in <glossterm linkend="pixels">pixels</glossterm>.</para></listitem>
381     </varlistentry>
382   </variablelist>
383 </sect3>
384 </sect2>
386 <sect2 id="random">
387 <title>A command that rolls dice for you</title>
388 <para>There is one command that rolls dice for you, it is called <userinput>random</userinput>, and it is very useful for some unexpected results.</para>
389   <variablelist>
390     <varlistentry> 
391       <term>random</term>
392       <listitem><para><screen>random X,Y</screen>
393       <userinput>random</userinput> 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:
394       <screen>
395 repeat 500 [
396   x = random 1,20
397   forward x
398   turnleft 10 - x
400 </screen>
401       Using the <userinput>random</userinput> command you can add a bit of chaos to your program.</para></listitem>
402     </varlistentry>
403   </variablelist>
404 </sect2>
406 <sect2 id="dialogs">
407 <title>Input and feedback though dialogs</title>
408 <para>A dialog is a small pop-up window that provides some feedback or asks for some input. &kturtle; has two commands for dialogs, namely: <userinput>message</userinput> and <userinput>inputwindow</userinput></para>
409 <sect3 id="message">
410   <title>message</title>
411   <variablelist>
412     <varlistentry> 
413       <term>message</term>
414       <listitem><para><screen>message X</screen>
415       The <userinput>message</userinput> command takes a <link linkend="string">string</link> as input. It shows a pop-up dialog containing the text from the <link linkend="string">string</link>.
416 <screen>
417 year = 2003
418 author = "Cies"
419 print author + " started the KTurtle project in " + year + " and still enjoys working on it!"
420 </screen>
421       </para></listitem>
422     </varlistentry>
423   </variablelist>
424 </sect3>
425 <sect3 id="inputwindow">
426   <title>inputwindow</title>
427   <variablelist>
428     <varlistentry> 
429       <term>inputwindow</term>
430       <listitem><para><screen>inputwindow X</screen>
431       <userinput>inputwindow</userinput> takes a <link linkend="string">string</link> as input. It shows a pop-up dialog containing the text from the string, just like the <link linkend="message">message</link>. But in addition to it also puts an input field on the dialog. Through this input filed the user can enter a <link linkend="number">number</link> or a <link linkend="string">string</link> which can be stored in a <link linkend="containers">container</link>. For example
432 <screen>
433 in = inputwindow "What is you age?"
434 out = 2003 - in
435 print "In 2003 you where " + out + " years old at some point."
436 </screen>
437       When a user cancels the input dialog, or does not enter anything at all the <link linkend="containers">container</link> is emptied.</para></listitem>
438     </varlistentry>
439   </variablelist>
440 </sect3>
441 </sect2>
443 </sect1>
447 <sect1 id="containers">
448 <title>Containers</title>
449 <para>Containers 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 <link linkend="variables">variables</link>, containers that can contain text are called <link linkend="string">string</link>.</para>
451 <para>Containers that are not used contain nothing. An example:
452 <screen>
453 print N
454 </screen>
455 This will print nothing. If we try to do <link linkend="math">math</link> with empty containers we will get errors.
456 </para>
458 <sect2 id="variables">
459 <title>Variables: number containers</title>
460 <para>Let us start with an example:
461 <screen>
462 x = 3
463 print x
464 </screen>
465 In the first line the letter <userinput>x</userinput> made into a variable (number container). As you see the value of the variable <userinput>x</userinput> is set to 3. On the second line the value is printed.</para>
466 <para> Note that if we wanted to print an <quote>x</quote> that we should have written
467 <screen>
468 print "x"
469 </screen>
470 </para>
471 <para>That was easy, now a bit harder example:
472 <screen>
473 A = 2004
474 B = 25
475 C = A + B
477 # the next command prints "2029"
478 print C
479 backward 30
480 # the next command prints "2004 plus 25"
481 print A + " plus " + B
482 backward 30
483 # the next command prints "1979"
484 print A - B
485 </screen>
486 In the first two lines the variables <userinput>A</userinput> and <userinput>B</userinput> are set to 2004 and 25. On the third line the variable <userinput>C</userinput> is set to <userinput>A + B</userinput>, which is 2029. The rest of the example consists of 3 <userinput>print</userinput> commands with <userinput>backward 30</userinput> in between. The <userinput>backward 30</userinput> is there to make sure every output is on a new line. In this example you also see that variables can be used in <link linkend="math">mathematical calculations</link>.</para>
487 </sect2>
489 <sect2 id="strings">
490 <title>Containers that contain text (strings)</title>
491 <para>In programming code the regular text is usually started and ended with quotes. As we have already seen:
492 <screen>
493 print "Hello programmer!"
494 </screen>
495 The regular is delimited with quotes. These pieces of regular text we call <link linkend="strings">strings</link>.</para>
496 <para>Strings can also be stored in <link linkend="containers">containers</link> just like <link linkend="number">numbers</link>
497 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 <link linkend="math">mathematical calculations</link> and <link linkend="questions">questions</link>. An example of the use of strings:
498 <screen>
499 x = "Hello "
500 name = inputwindow "Please enter your name..."
501 print x + name + ", how are you?"
502 </screen>
503 On the first line the string <userinput>x</userinput> is set to <quote>Hello </quote>. On the second line the string <userinput>name</userinput> is set to the output of the <userinput>inputwindow</userinput> command. On the third line the program prints a composition of three strings on the canvas.</para>
504 <para>This program ask you to enter your name. When you, for instance, enter the name <quote>Paul</quote>, the program prints <quote>Hello Paul, how are you?</quote>. Please note that the plus (+) is the only math symbol that you can use with strings.</para>
505 </sect2>
506 </sect1>
508 <sect1 id="math">
509 <title>Can the Turtle do math?</title>
510 <para>Yes, &kturtle; will do your math. You can add (+), substract (-), multiply (*), and divide (/). Here is an example in which we use all of them:
511 <screen>
512 a = 20 - 5
513 b = 15 * 2
514 c = 30 / 30
515 d = 1 + 1
516 print "a: "+a+", b: "+b+", c: "+c+", d: "+d 
517 </screen>
518 Do you know what value a, b, c and d have? Please note the use of the <link linkend="assignment">assignment</link> symbol <userinput>=</userinput>.</para>
519 <para>If you just want a simple calculation to be done you can do something like this:
520 <screen>
521 print 2004-12
522 </screen></para>
523 <para>Now an example with parentheses:
524 <screen>
525 print ( ( 20 - 5 ) * 2 / 30 ) + 1
526 </screen>
527 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).</para>
528 </sect1>
530 <sect1 id="questions">
531 <title>Asking questions, getting answers...</title>
532 <para><link linkend="if"><userinput>if</userinput></link> and <link linkend="while"><userinput>while</userinput></link> are <link linkend="controlling-execution">execution controllers</link> that we will discuss in the next section. In this section we use the <link linkend="if"><userinput>if</userinput></link> command to explain questions.</para>
533 <sect2 id="q">
534 <title>Questions</title>
535 <para>A simple example of a question:
536 <screen>
537 x = 6
538 if x &gt; 5 [
539   print "hello"
541 </screen>
543 In this example the question is the <userinput>x &gt; 5</userinput> 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 <link linkend="controlling-execution">execution controllers</link>, like <link linkend="if"><userinput>if</userinput></link>. All numbers and <link linkend="variables">variables</link> (number containers) can be compared to each other with questions.</para>
544 <para>
545 Here are all possible questions:
546 <table>
547 <title>Types of questions</title>
548 <tgroup cols="3">
549 <tbody>
550 <row>
551 <entry><userinput>a == b</userinput></entry>
552 <entry>equals</entry>
553 <entry>answer is <quote>true</quote> if <userinput>a</userinput> equals <userinput>b</userinput></entry>
554 </row>
555 <row>
556 <entry><userinput>a != b</userinput></entry>
557 <entry>not-equal</entry>
558 <entry>answer is <quote>true</quote> if <userinput>a</userinput> does not equal <userinput>b</userinput></entry>
559 </row>
560 <row>
561 <entry><userinput>a &gt; b</userinput></entry>
562 <entry>greater than</entry>
563 <entry>answer is <quote>true</quote> if <userinput>a</userinput> is greater than <userinput>b</userinput></entry>
564 </row>
565 <row>
566 <entry><userinput>a &lt; b</userinput></entry>
567 <entry>smaller than</entry>
568 <entry>answer is <quote>true</quote> if <userinput>a</userinput> is smaller than <userinput>b</userinput></entry>
569 </row>
570 <row>
571 <entry><userinput>a &gt;= b</userinput></entry>
572 <entry>greater than or equals</entry>
573 <entry>answer is <quote>true</quote> if <userinput>a</userinput> is greater than or equals <userinput>b</userinput></entry>
574 </row>
575 <row>
576 <entry><userinput>a &lt;= b</userinput></entry>
577 <entry>smaller than or equals</entry>
578 <entry>answer is <quote>true</quote> if <userinput>a</userinput> is smaller than or equals <userinput>b</userinput></entry>
579 </row>
580 </tbody>
581 </tgroup>
582 </table>
583 </para>
584 <para>Questions are <glossterm>highlighted</glossterm> with light blue in the <link linkend="the-code-editor">code editor</link>.</para> 
585 </sect2>
587 <sect2 id="question-glue">
588 <title>Question Glue</title>
589 <para>
590 Question glue-words enable us to glue questions into one big question.
591 <screen>
592 a = 1
593 b = 5
594 if (a &lt; 5) and (b == 5) [
595   print "hello"
597 </screen>
598 In this example the glue-word <userinput>and</userinput> is used to glue 2 questions (<userinput>a &lt; 5</userinput>, <userinput>b == 5</userinput>) together. If one side of the <userinput>and</userinput> would answer <quote>false</quote> the whole question would answer <quote>false</quote>, because with the glue-word <userinput>and</userinput> both sides need to be <quote>true</quote> in order to answer <quote>true</quote>. Please do not forget to use the brackets around the questions!</para>
600 <para>
601 Here is a schematic overview; a more detailed explanation follows below:
602 <table>
603 <title>Question glue-words</title>
604 <tgroup cols="2">
605 <tbody>
606 <row>
607 <entry><userinput>and</userinput></entry>
608 <entry>Both sides need to be 'true' in order to answer 'true'</entry>
609 </row>
610 <row>
611 <entry><userinput>or</userinput></entry>
612 <entry>If one of the sides is 'true' the answer is 'true'</entry>
613 </row>
614 <row>
615 <entry><userinput>not</userinput></entry>
616 <entry>Special case: only works on one question! Changes 'true' into 'false' and 'false' into 'true'.</entry>
617 </row>
618 </tbody>
619 </tgroup>
620 </table>
621 </para>
622 <para>Question glue-words are <glossterm>highlighted</glossterm> with purple in the <link linkend="the-code-editor">code editor</link>.</para>
624 <sect3 id="and">
625 <title>and</title>
626 <para>When two questions are glued together with <userinput>and</userinput>, both sides of the <userinput>and</userinput> have to be 'true' in order to result in 'true'. An example:
627 <screen>
628 a = 1
629 b = 5
630 if ((a &lt; 10) and (b == 5)) and (a &lt; b) [
631   print "hello"
633 </screen>
634 In this example you see a glued question glued onto an other question.</para>
635 </sect3>
637 <sect3 id="or">
638 <title>or</title>
639 <para>If one of the two questions that are glued together with <userinput>or</userinput> is 'true' the result will be 'true'. An example:
640 <screen>
641 a = 1
642 b = 5
643 if ((a &lt; 10) or (b == 10)) or (a == 0) [
644   print "hello"
646 </screen>
647 In this example you see a glued question glued onto an other question.</para>
648 </sect3>
650 <sect3 id="not">
651 <title>not</title>
652 <para><userinput>not</userinput> is a special question glue-word because it only works for one question at the time. <userinput>not</userinput> changes 'true' into 'false' and 'false' into 'true'. An example:
653 <screen>
654 a = 1
655 b = 5
656 if not ((a &lt; 10) and (b == 5)) [
657   print "hello"
659 else
661   print "not hello ;-)"
663 </screen>
664 In this example the glued question is 'true' yet the <userinput>not</userinput> changes it to 'false'. So in the end <userinput>"not hello ;-)"</userinput> is printed on the <link linkend="the-canvas">canvas</link>.</para>
665 </sect3>
667 </sect2>
669 </sect1>
671 <sect1 id="controlling-execution">
672 <title>Controlling execution</title>
673 <para>The execution controllers enable you &mdash; as their name implies &mdash; to control execution.</para>
674 <para>Execution controlling commands are <glossterm>highlighted</glossterm> with dark green in a bold font type. The square brackets are mostly used together with execution controllers and they are <glossterm>highlighted</glossterm> with light green.</para>
676 <sect2 id="wait">
677 <title>Have the turtle wait</title>
678 <para>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.</para>
679   <variablelist>
680     <varlistentry>
681       <term>wait</term>
682       <listitem><para><screen>wait X</screen>
683       <userinput>wait</userinput> makes the turtle wait for X seconds.
684 <screen>
685 repeat 36 [
686   forward 5
687   turnright 10
688   wait 0.5
690 </screen>
691       This code draws a circle, but the turtle will wait half a second
692       after each step. This gives the impression of a slow-moving turtle.</para></listitem>
693     </varlistentry>
694   </variablelist>
695 </sect2>
697 <sect2 id="if">
698 <title>Execute "if"</title>
699 <para></para>
700   <variablelist>
701     <varlistentry>
702       <term>if</term>
703       <listitem><para><screen>if <link linkend="questions">question</link> [ ... ]</screen>
704       The code that is placed between the brackets will only be executed <userinput>if</userinput> the answer to the <link linkend="questions">question</link> is <quote>true</quote>. Please read for more information on <link linkend="questions">questions</link> in the <link linkend="questions">question section</link>.
705       <screen>
706 x = 6
707 if x &gt; 5 [
708   print "x is greater than five!"
710 </screen>
711       On the first line <userinput>x</userinput> is set to 6. On the second line the <link linkend="questions">question</link> <userinput>x &gt; 5</userinput> is asked. Since the answer to this question is <quote>true</quote> the execution controller <userinput>if</userinput> will allow the code between the brackets to be executed</para></listitem>
712     </varlistentry>
713   </variablelist>
714 </sect2>
716 <sect2 id="while">
717 <title>The "while" loop</title>
718 <para></para>
719   <variablelist>
720     <varlistentry>
721       <term>while</term>
722       <listitem><para><screen>while <link linkend="questions">question</link> [ ... ]</screen>
723       The execution controller <userinput>while</userinput> is a lot like <link linkend="if"><userinput>if</userinput></link>. The difference is that <userinput>while</userinput> keeps repeating (looping) the code between the brackets until the answer to the <link linkend="questions">question</link> is <quote>false</quote>.
724       <screen>
725 x = 1
726 while x &lt; 5 [
727   forward 10
728   wait 1
729   x = x + 1
731 </screen>
732       On the first line <userinput>x</userinput> is set to 1. On the second line the <link linkend="questions">question</link> <userinput>x &lt; 5</userinput> is asked. Since the answer to this question is <quote>true</quote> the execution controller <userinput>while</userinput> starts executing the code between the brackets until the answer to the <link linkend="questions">question</link> is <quote>false</quote>. In this case the code between the brackets will be executed 4 times, because every time the fifth line is executed <userinput>x</userinput> increases by 1.</para></listitem>
733     </varlistentry>
734   </variablelist>
735 </sect2>
737 <sect2 id="else">
738 <title>If not, in other words: "else"</title>
739 <para></para>
740   <variablelist>
741     <varlistentry>
742       <term>else</term>
743       <listitem><para><screen>if question [ ... ] else [ ... ]</screen>
744       <userinput>else</userinput> can be used in addition to the execution controller <link linkend="if"><userinput>if</userinput></link>. The code between the brackets after <userinput>else</userinput> is only executed if the answer to the <link linkend="questions">question</link> that is asked is <quote>false</quote>. 
745       <screen>
746 reset
747 x = 4
748 if x &gt; 5 [
749   print "x is greater than five!"
751 else
753   print "x is smaller than six!"
755 </screen>
756       The <link linkend="questions">question</link> asks if <userinput>x</userinput> is greater than 5. Since <userinput>x</userinput> is set to 4 on the first line the answer to the question is <quote>false</quote>. This means the code between the brackets after <userinput>else</userinput> gets executed.</para></listitem>
757     </varlistentry>
758   </variablelist>
759 </sect2>
761 <sect2 id="for">
762 <title>The "for" loop, a counting loop</title>
763 <para></para>
764   <variablelist>
765     <varlistentry>
766       <term>for</term>
767       <listitem><para><screen>for <userinput>start point</userinput> to <userinput>end point</userinput> [ ... ]</screen>
768       The <userinput>for</userinput> loop is a <quote>counting loop</quote>, &ie; it keeps count for you.
769       <screen>
770 for x = 1 to 10 [
771   print x * 7
772   forward 15
774 </screen>
775      Every time the code between the brackets is executed the <userinput>x</userinput> is increased by 1, until <userinput>x</userinput> reaches the value of 10. The code between the brackets prints the <userinput>x</userinput> multiplied by 7. After this program finishes its execution you will see the times table of 7 on the canvas.</para></listitem>
776     </varlistentry>
777   </variablelist>
778 </sect2>
780 </sect1>
783 <sect1 id="learn">
784 <title>Create your own commands with <quote>learn</quote></title>
785 <para><userinput>learn</userinput> is a very special command, because it is used to create your own commands. The command you create can take <glossterm linkend="input-output">input</glossterm> and return <glossterm linkend="input-output">output</glossterm>. Let us take a look at how a new command is created:
786 <screen>
787 learn circle x [
788   repeat 36 [
789     forward x
790     turnleft 10
791   ]
793 </screen>
794 The new command is called <userinput>circle</userinput>. <userinput>circle</userinput> takes one <glossterm linkend="input-output">input</glossterm>, a number, to set the size of the circle. <userinput>circle</userinput> returns no <glossterm linkend="input-output">output</glossterm>. The <userinput>circle</userinput> command can now be used like a normal command in the rest of the code. See this example:
795 <screen>
796 learn circle X [
797   repeat 36 [ 
798     forward X 
799     turnleft 10 
800   ] 
803 go 30,30 
804 circle 20
806 go 40,40 
807 circle 50  
808 </screen>
809 </para>
810 <para>In the next example, a command with a return value is created.
811 <screen>
812 reset
814 learn multiplyBySelf n [
815   r = n * 1
816   r = n * n
817   return r
819 i = inputwindow "Please enter a number and press OK"
820 print i + " multiplied by itself is: " + multiplyBySelf i
821 </screen>
822 In this example a new command called <userinput>multiplyBySelf</userinput> is created. The input of this command is multiplied by itself and then returned, using the <anchor id="return" /><userinput>return</userinput> command. The <userinput>return</userinput> command is the way to output a value from a function you have created.
823 </para>
824 </sect1>
826 </chapter>