Sophie

Sophie

distrib > Mandriva > 2009.0 > i586 > by-pkgid > ba593aaf4fde116859478f9201665276 > files > 14

kturtle-4.2.4-0.1mdv2009.0.i586.rpm

<!--Dear translator: please NEVER translate the id or anything inside the tags as they are needed in english by the application
     Thanks a lot in advance.-->
<chapter id="reference">
<title>&turtlelang; Programming Reference</title>
<para>This is the reference for &kturtle;'s &turtlelang;. 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>

<sect1 id="different-instructions">
<title>Different Instruction Types</title>
<para>As in any language, &turtlelang; has different types of words and symbols. Here the differences between the types are briefly explained.</para>

<sect2 id="command">
<title>Commands</title>
<para>Using commands you tell the turtle or &kturtle; to do something. Some commands need input, some give output.
<screen>
# forward is a command that needs input, in this case the number 100:
forward 100
</screen>
</para>
<para>For a detailed overview of all commands that &kturtle; supports go <link linkend="commands">here</link>.</para>
</sect2>

<sect2 id="number">
<title>Numbers</title>
<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>
<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>.
</para>
<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>
<para>Numbers are <glossterm>highlighted</glossterm> with dark red in the <link linkend="the-code-editor">code editor</link>.</para>
</sect2>

<!-- constants like pi? -->

<sect2 id="string">
<title>Strings</title>
<para>First an example:
<screen>
print "Hello, I'm a string."
</screen>
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>
<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>
<para>Strings are <glossterm>highlighted</glossterm> with red in the <link linkend="the-code-editor">code editor</link>.</para>
</sect2>


<sect2 id="name">
<title>Names</title>
<para>When using the &turtlelang; 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 new command with <link linkend="learn">learn</link> you will have to specify a name.</para>
<para>You can choose any name, as long as it does not already have a meaning. For instance you cannot name a function <link linkend="forward">forward</link>, since that name is already used for an internal command.
<screen>
# here forward is used as a new command, 
# but it already has a meaning so 
# this will produce an error:
learn forward {
  print "this is invalid"
}

# this works:
learn myforward {
  print "this is ok"
}
</screen>
Names can contain only letters, numbers and underscores (_). Yet they have to start with a letter. Container names have to start with the container prefix ($).
<screen>
# here forward is used as a container, 
# starting with the $ prefix, so it does
# not conflict with the forward command
$forward = 20
print $forward
</screen>
</para>
<para>Containers are <glossterm>highlighted</glossterm> with bolded purple in the <link linkend="the-code-editor">code editor</link>.</para>
<para>
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.
</para>
</sect2>

<sect2 id="assignment">
<title>Assignments</title>
<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>
<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:
<screen>
$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
</screen>
</para>
<para>For more examples see the section that explains <link linkend="containers">containers</link>.</para>
</sect2>

<sect2 id="math-symbols">
<title>Math Symbols</title>
<para>&kturtle; supports all basic math symbols: add (<userinput>+</userinput>), subtract (<userinput>-</userinput>), multiply (<userinput>*</userinput>), divide (<userinput>/</userinput>) and the brackets <userinput>(</userinput> and <userinput>)</userinput>.</para>
<para>For a complete explanation and more examples see the <link linkend="math">math</link> section.</para>
</sect2>

<sect2 id="question">
<title>Questions</title>
<para>We can ask simple questions on which the answer will be 'true' or 'false'.</para>
<para>Using questions is extensively explained in the <link linkend="questions">questions</link> section.</para>
</sect2>

<sect2 id="questions-glue">
<title>Question Glue-Words</title>
<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>
<para>Using question-glue is explained in the <link linkend="question-glue">Question Glue</link> section.</para>
</sect2>


<sect2 id="comment">
<title>Comments</title>
<para>Comments are lines that start with a <userinput>#</userinput>. For example:
<screen>
# 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"
</screen>
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>
<para>Commented lines are <glossterm>highlighted</glossterm> with light grey in the <link linkend="the-code-editor">code editor</link>.</para>
</sect2>

</sect1>


<sect1 id="commands">
<title>Commands</title>
<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 blue in the <link linkend="the-code-editor">code editor</link>, this can help you to distinguish them.</para>

<sect2 id="moving-the-turtle">
<title>Moving the turtle</title>
<para>There are several commands to move the turtle over the screen.</para>

  <variablelist>
    <anchor id="forward" />
    <varlistentry> 
      <term>forward (fw)<indexterm><primary>forward (fw)</primary></indexterm></term>
      <listitem><para><screen>forward X</screen>
      <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>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="backward" />
    <varlistentry>  
      <term>backward (bw)<indexterm><primary>backward (bw)</primary></indexterm></term>
      <listitem><para><screen>backward X</screen>
      <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>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="turnleft" />
    <varlistentry> 
      <term>turnleft (tl)<indexterm><primary>turnleft (tl)</primary></indexterm></term>
      <listitem><para><screen>turnleft X</screen>
      <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>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="turnright" />
    <varlistentry> 
      <term>turnright (tr)<indexterm><primary>turnright (tr)</primary></indexterm></term>
      <listitem><para><screen>turnright X</screen>
      <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>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="direction" />
    <varlistentry> 
      <term>direction (dir)<indexterm><primary>direction (dir)</primary></indexterm></term>
      <listitem><para><screen>direction X</screen>
      <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>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="center" />
    <varlistentry> 
      <term>center<indexterm><primary>center</primary></indexterm></term>
      <listitem><para><screen>center</screen>
      <userinput>center</userinput> moves the turtle to the center on the canvas.</para></listitem>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="go" />
    <varlistentry> 
      <term>go<indexterm><primary>go</primary></indexterm></term>
      <listitem><para><screen>go X,Y</screen>
      <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. </para></listitem>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="gox" />
    <varlistentry> 
      <term>gox<indexterm><primary>gox</primary></indexterm></term>
      <listitem><para><screen>gox X</screen>
      <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>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="goy" />
    <varlistentry> 
      <term>goy<indexterm><primary>goy</primary></indexterm></term>
      <listitem><para><screen>goy Y</screen>
      <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>
    </varlistentry>
  </variablelist>
  <note><para>Using the commands <userinput>go</userinput>, <userinput>gox</userinput>, <userinput>goy</userinput> and <userinput>center</userinput> the turtle will not draw a line, no matter if the pen is up or down.</para>
  </note>
</sect2>

<sect2 id="locate-the-turtle">
<title>Where is the turtle?</title>
<para>There are two commands which return the position of the turtle on the screen.</para>

  <variablelist>
    <anchor id="getx" />
    <varlistentry> 
      <term>getx<indexterm><primary>getx</primary></indexterm></term>
      <listitem><para>
      <userinput>getx</userinput> returns the number of pixels from the left of the canvas to the current position of the turtle.</para></listitem>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="gety" />
    <varlistentry> 
      <term>gety<indexterm><primary>gety</primary></indexterm></term>
      <listitem><para>
      <userinput>gety</userinput> returns the number of pixels from the top of the canvas to the current position of the turtle.</para></listitem>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="pen">
<title>The turtle has a pen</title>
<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>
  <variablelist>
    <anchor id="penup" />
    <varlistentry> 
      <term>penup (pu)<indexterm><primary>penup (pu)</primary></indexterm></term>
      <listitem><para><screen>penup</screen>
      <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>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="pendown" />
    <varlistentry> 
      <term>pendown (pd)<indexterm><primary>pendown (pd)</primary></indexterm></term>
      <listitem><para><screen>pendown</screen>
      <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>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="setpenwidth" />
    <varlistentry> 
      <term>penwidth (pw)<indexterm><primary>penwidth (pw)</primary></indexterm></term>
      <listitem><para><screen>penwidth X</screen>
      <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>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="setfgcolor" />
    <varlistentry> 
      <term>pencolor (pc)<indexterm><primary>pencolor (pc)</primary></indexterm></term>
      <listitem><para><screen>pencolor R,G,B</screen>
      <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>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="canvas">
<title>Commands to control the canvas</title>
<para>There are several commands to control the canvas.</para>
  <variablelist>
    <anchor id="resizecanvas" />
    <varlistentry>
      <term>canvassize (cs)<indexterm><primary>canvassize (cs)</primary></indexterm></term>
      <listitem><para><screen>canvassize X,Y</screen>
      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>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="setbgcolor" />
    <varlistentry> 
      <term>canvascolor (cc)<indexterm><primary>canvascolor (cc)</primary></indexterm></term>
      <listitem><para><screen>canvascolor R,G,B</screen>
      <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>
    </varlistentry>
  </variablelist>
<!-- <sect3 id="wrapon">
  <title>wrapon</title>
  <variablelist>
    <varlistentry> 
      <term>wrapon</term>
      <listitem><para><screen>wrapon</screen>
      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>
    </varlistentry>
  </variablelist>
</sect3>
<sect3 id="wrapoff">
  <title>wrapoff</title>
  <variablelist>
    <varlistentry> 
      <term>wrapoff</term>
      <listitem><para><screen>wrapoff</screen>
      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>
    </varlistentry>
  </variablelist>
</sect3>  -->
</sect2>

<sect2 id="clean">
<title>Commands to clean up</title>
<para>There are two commands to clean up the canvas after you have made a mess.</para>
  <variablelist>
    <anchor id="clear" />
    <varlistentry> 
      <term>clear (ccl)<indexterm><primary>clear (ccl)</primary></indexterm></term>
      <listitem><para><screen>clear</screen>
      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.</para></listitem>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="reset" />
    <varlistentry> 
      <term>reset<indexterm><primary>reset</primary></indexterm></term>
      <listitem><para><screen>reset</screen>
      <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, the turtle draws a black line on the canvas and the canvassize is set to 400 x 400 pixels.</para></listitem>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="sprites">
<title>The turtle is a sprite</title>
<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>
<para>Next you will find a full overview on all commands to work with sprites.</para>
<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>
  <variablelist>
    <anchor id="spriteshow" />
    <varlistentry> 
      <term>spriteshow (ss)<indexterm><primary>spriteshow (ss)</primary></indexterm></term>
      <listitem><para><screen>spriteshow</screen>
      <userinput>spriteshow</userinput> makes the turtle visible again after it has been hidden. <userinput>spriteshow</userinput> can be abbreviated to <userinput>ss</userinput>.</para></listitem>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="spritehide" />
    <varlistentry> 
      <term>spritehide (sh)<indexterm><primary>spritehide (sh)</primary></indexterm></term>
      <listitem><para><screen>spritehide</screen>
      <userinput>spritehide</userinput> hides the turtle. This can be used if the turtle does not fit in your drawing. <userinput>spritehide</userinput> can be abbreviated to <userinput>sh</userinput>.</para></listitem>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="writing">
<title>Can the turtle write?</title>
<para>The answer is: <quote>yes</quote>. The turtle can write: it writes just about everything you command it to.</para>
  <variablelist>
    <anchor id="print" />
    <varlistentry> 
      <term>print<indexterm><primary>print</primary></indexterm></term>
      <listitem><para><screen>print X</screen>
      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:
<screen>
$year = 2003
$author = "Cies"
print $author + " started the KTurtle project in " + $year + " and still enjoys working on it!"
</screen>
      </para></listitem>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="fontsize" />
    <varlistentry> 
      <term>fontsize<indexterm><primary>fontsize</primary></indexterm></term>
      <listitem><para><screen>fontsize X</screen>
      <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>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="random">
<title>A command that rolls dice for you</title>
<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>
  <variablelist>
    <varlistentry> 
      <term>random (rnd)<indexterm><primary>random (rnd)</primary></indexterm></term>
      <listitem><para><screen>random X,Y</screen>
      <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 than the minimum and equal or smaller than the maximum. Here a small example:
      <screen>
repeat 500 {
  $x = random 1,20
  forward $x
  turnleft 10 - $x
}
</screen>
      Using the <userinput>random</userinput> command you can add a bit of chaos to your program.</para></listitem>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="dialogs">
<title>Input and feedback though dialogs</title>
<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>ask</userinput></para>
  <variablelist>
    <anchor id="message" />
    <varlistentry> 
      <term>message<indexterm><primary>message</primary></indexterm></term>
      <listitem><para><screen>message X</screen>
      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>.
<screen>
message "Cies started the KTurtle project in 2003 and still enjoys working on it!"
</screen>
      </para></listitem>
    </varlistentry>
  </variablelist>
  <variablelist>
    <anchor id="ask" />
    <varlistentry> 
      <term>ask<indexterm><primary>ask</primary></indexterm></term>
      <listitem><para><screen>ask X</screen>
      <userinput>ask</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
<screen>
$in = ask "What is you age?"
$out = 2003 - $in
print "In 2003 you where " + $out + " years old at some point."
</screen>
      When a user cancels the input dialog, or does not enter anything at all the <link linkend="containers">container</link> is emptied.</para></listitem>
    </varlistentry>
  </variablelist>
</sect2>

</sect1>



<sect1 id="containers">
<title>Containers</title>
<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>. Containers can be identified by the container character <quote>$</quote> that precedes their usage.</para>

<para>Containers that are not used contain nothing. An example:
<screen>
print $N
</screen>
This will print nothing and you get an error message.
</para>

<sect2 id="variables">
<title>Variables: number containers</title>
<para>Let us start with an example:
<screen>
$x = 3
print $x
</screen>
In the first line the letter <userinput>x</userinput> is 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>
<para> Note that if we wanted to print an <quote>x</quote> that we should have written
<screen>
print "x"
</screen>
</para>
<para>That was easy, now a bit harder example:
<screen>
$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
</screen>
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>
</sect2>

<sect2 id="strings">
<title>Containers that contain text (strings)</title>
<para>In programming code the regular text is usually started and ended with quotes. As we have already seen:
<screen>
print "Hello programmer!"
</screen>
The regular is delimited with quotes. These pieces of regular text we call <link linkend="strings">strings</link>.</para>
<para>Strings can also be stored in <link linkend="containers">containers</link> just like <link linkend="number">numbers</link>
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:
<screen>
$x = "Hello "
$name = ask "Please enter your name..."
print $x + $name + ", how are you?"
</screen>
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>ask</userinput> command. On the third line the program prints a composition of three strings on the canvas.</para>
<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>
</sect2>
</sect1>

<sect1 id="math">
<title>Can the Turtle do math?</title>
<para>Yes, &kturtle; will do your math. You can add (+), subtract (-), multiply (*), and divide (/). Here is an example in which we use all of them:
<screen>
$a = 20 - 5
$b = 15 * 2
$c = 30 / 30
$d = 1 + 1
print "a: "+$a+", b: "+$b+", c: "+$c+", d: "+$d 
</screen>
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>
<para>If you just want a simple calculation to be done you can do something like this:
<screen>
print 2004-12
</screen></para>
<para>Now an example with parentheses:
<screen>
print ( ( 20 - 5 ) * 2 / 30 ) + 1
</screen>
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>
<para>&kturtle; has advanced mathematical features. It knows the number <userinput>pi</userinput> and trigonometrical functions like <userinput>sin</userinput>, <userinput>cos</userinput>, <userinput>tan</userinput>, <userinput>arcsin</userinput>, <userinput>arccos</userinput>, <userinput>arctan</userinput> and the functions <userinput>sqrt</userinput> and <userinput>exp</userinput>.
<indexterm><primary>pi</primary></indexterm>
<indexterm><primary>sin, cos, tan</primary></indexterm>
<indexterm><primary>arcsin, arccos, arctan</primary></indexterm>
<indexterm><primary>sqrt, exp</primary></indexterm>
</para>

<sect2 id="round">
<title>Let the turtle round your numbers</title>
<para>If you need integers in calculations, use this function.</para>
  <variablelist>
    <varlistentry>
      <term>round<indexterm><primary>round</primary></indexterm></term>
      <listitem><para><screen>round(x)</screen>
      <userinput>round</userinput> the given number to the nearest integer.
<screen>
print round(10.8)
forward 20
print round(10.3)
forward 20
</screen>
      With this code the turtle will print the numbers 11 and 10.</para></listitem>
    </varlistentry>
  </variablelist>
</sect2>
</sect1>

<sect1 id="questions">
<title>Asking questions, getting answers...</title>
<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>
<sect2 id="q">
<title>Questions</title>
<para>A simple example of a question:
<screen>
$x = 6
if $x &gt; 5 {
  print "hello"
}
</screen>

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>
<para>
Here are all possible questions:
<table>
<title>Types of questions</title>
<tgroup cols="3">
<tbody>
<row>
<entry><userinput>a == b</userinput></entry>
<entry>equals</entry>
<entry>answer is <quote>true</quote> if <userinput>a</userinput> equals <userinput>b</userinput></entry>
</row>
<row>
<entry><userinput>a != b</userinput></entry>
<entry>not-equals</entry>
<entry>answer is <quote>true</quote> if <userinput>a</userinput> does not equal <userinput>b</userinput></entry>
</row>
<row>
<entry><userinput>a &gt; b</userinput></entry>
<entry>greater than</entry>
<entry>answer is <quote>true</quote> if <userinput>a</userinput> is greater than <userinput>b</userinput></entry>
</row>
<row>
<entry><userinput>a &lt; b</userinput></entry>
<entry>smaller than</entry>
<entry>answer is <quote>true</quote> if <userinput>a</userinput> is smaller than <userinput>b</userinput></entry>
</row>
<row>
<entry><userinput>a &gt;= b</userinput></entry>
<entry>greater than or equals</entry>
<entry>answer is <quote>true</quote> if <userinput>a</userinput> is greater than or equals <userinput>b</userinput></entry>
</row>
<row>
<entry><userinput>a &lt;= b</userinput></entry>
<entry>smaller than or equals</entry>
<entry>answer is <quote>true</quote> if <userinput>a</userinput> is smaller than or equals <userinput>b</userinput></entry>
</row>
</tbody>
</tgroup>
</table>
</para>
</sect2>

<sect2 id="question-glue">
<title>Question Glue</title>
<para>
Question glue-words enable us to glue questions into one big question.
<screen>
$a = 1
$b = 5
if ($a &lt; $b) and ($b == 5) {
  print "hello"
}
</screen>
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>

<para>
Here is a schematic overview; a more detailed explanation follows below:
<table>
<title>Question glue-words</title>
<tgroup cols="2">
<tbody>
<row>
<entry><userinput>and</userinput></entry>
<entry>Both sides need to be 'true' in order to answer 'true'</entry>
</row>
<row>
<entry><userinput>or</userinput></entry>
<entry>If one of the sides is 'true' the answer is 'true'</entry>
</row>
<row>
<entry><userinput>not</userinput></entry>
<entry>Special case: only works on one question! Changes 'true' into 'false' and 'false' into 'true'.</entry>
</row>
</tbody>
</tgroup>
</table>
</para>

<sect3 id="and">
<title>and</title>
<indexterm><primary>and</primary></indexterm>
<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:
<screen>
$a = 1
$b = 5
if (($a &lt; 10) and ($b == 5)) and ($a &lt; $b) {
  print "hello"
}
</screen>
In this example you see a glued question glued onto an other question.</para>
</sect3>

<sect3 id="or">
<title>or</title>
<indexterm><primary>or</primary></indexterm>
<para>If one of the two questions that are glued together with <userinput>or</userinput> is 'true' the result will be 'true'. An example:
<screen>
$a = 1
$b = 5
if (($a &lt; 10) or ($b == 10)) or ($a == 0) {
  print "hello"
}
</screen>
In this example you see a glued question glued onto an other question.</para>
</sect3>

<sect3 id="not">
<title>not</title>
<indexterm><primary>not</primary></indexterm>
<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:
<screen>
$a = 1
$b = 5
if not (($a &lt; 10) and ($b == 5)) {
  print "hello"
}
else
{
  print "not hello ;-)"
}
</screen>
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>
</sect3>

</sect2>

</sect1>

<sect1 id="controlling-execution">
<title>Controlling execution</title>
<para>The execution controllers enable you &mdash; as their name implies &mdash; to control execution.</para>
<para>Execution controlling commands are <glossterm>highlighted</glossterm> with dark green in a bold font type. The brackets are mostly used together with execution controllers and they are <glossterm>highlighted</glossterm> with bolded black.</para>

<sect2 id="wait">
<title>Have the turtle wait</title>
<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>
  <variablelist>
    <varlistentry>
      <term>wait<indexterm><primary>wait</primary></indexterm></term>
      <listitem><para><screen>wait X</screen>
      <userinput>wait</userinput> makes the turtle wait for X seconds.
<screen>
repeat 36 {
  forward 5
  turnright 10
  wait 0.5
}
</screen>
      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.</para></listitem>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="if">
<title>Execute "if"</title>
  <variablelist>
    <varlistentry>
      <term>if<indexterm><primary>if</primary></indexterm></term>
      <listitem><para><screen>if <link linkend="questions">question</link> { ... }</screen>
      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>.
      <screen>
$x = 6
if $x &gt; 5 {
  print "x is greater than five!"
}
</screen>
      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>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="else">
<title>If not, in other words: "else"</title>
  <variablelist>
    <varlistentry>
      <term>else<indexterm><primary>else</primary></indexterm></term>
      <listitem><para><screen>if question { ... } else { ... }</screen>
      <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>. 
      <screen>
reset
$x = 4
if $x &gt; 5 {
  print "x is greater than five!"
}
else
{
  print "x is smaller than six!"
}
</screen>
      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>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="while">
<title>The "while" loop</title>
  <variablelist>
    <varlistentry>
      <term>while<indexterm><primary>while</primary></indexterm></term>
      <listitem><para><screen>while <link linkend="questions">question</link> { ... }</screen>
      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>.
      <screen>
$x = 1
while $x &lt; 5 {
  forward 10
  wait 1
  $x = $x + 1
}
</screen>
      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>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="repeat">
<title>The "repeat" loop</title>
  <variablelist>
    <varlistentry>
      <term>repeat<indexterm><primary>repeat</primary></indexterm></term>
      <listitem><para><screen>repeat number { ... }</screen>
      The execution controller <userinput>repeat</userinput> is a lot like <link linkend="while"><userinput>while</userinput></link>. The difference is that <userinput>repeat</userinput> keeps repeating (looping) the code between the brackets for the given number.</para></listitem>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="for">
<title>The "for" loop, a counting loop</title>
  <variablelist>
    <varlistentry>
      <term>for<indexterm><primary>for</primary></indexterm><indexterm><primary>step</primary></indexterm></term>
      <listitem><para><screen>for <userinput>start point</userinput> to <userinput>end point</userinput> { ... }</screen>
      The <userinput>for</userinput> loop is a <quote>counting loop</quote>, &ie; it keeps count for you.
      <screen>
for $x = 1 to 10 {
  print $x * 7
  forward 15
}
</screen>
     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>
     <para>
     The default step size of a loop is 1, you can use an other value with
     <screen>for <userinput>start point</userinput> to <userinput>end point</userinput> step <userinput>step size</userinput> { ... }</screen></para></listitem>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="break">
<title>Leave a loop</title>
  <variablelist>
    <varlistentry>
      <term>break<indexterm><primary>break</primary></indexterm></term>
      <listitem><para><screen>break</screen>
      Terminates the current loop immediately and transfers control to the statement immediately following that loop</para></listitem>
    </varlistentry>
  </variablelist>
</sect2>

<sect2 id="exit">
<title>Stop the turtle</title>
  <variablelist>
    <varlistentry>
      <term>exit<indexterm><primary>exit</primary></indexterm></term>
      <listitem><para><screen>exit</screen>
      Finishes the execution of the code.</para></listitem>
    </varlistentry>
  </variablelist>
</sect2>
</sect1>


<sect1 id="learn">
<title>Create your own commands with <quote>learn</quote></title>
<para><userinput>learn</userinput><indexterm><primary>learn</primary></indexterm> 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:
<screen>
learn circle $x {
  repeat 36 {
    forward $x
    turnleft 10
  }
}
</screen>
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:
<screen>
learn circle $X {
  repeat 36 {
    forward $X 
    turnleft 10 
  }
}

go 200,200 
circle 20

go 300,200 
circle 40  
</screen>
</para>
<para>In the next example, a command with a return value is created.
<screen>
reset

learn multiplyBySelf $n {
  $r = $n * $n
  return $r
}
$i = ask "Please enter a number and press OK"
print $i + " multiplied by itself is: " + multiplyBySelf $i
</screen>
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.
</para>
<para>Commands can have more than one <glossterm linkend="input-output">input</glossterm>. In the next example, a command that draws a rectangle is created.
<screen>
learn box $X, $Y {
  forward $Y
  turnright 90
  forward $X
  turnright 90
  forward $Y
  turnright 90
  forward $X
  turnright 90
}
</screen>
Now you can run <userinput>box 50, 100</userinput> and the turtle will draw a rectangle on the canvas. 
</para>
  
</sect1>

</chapter>